两个流浪的vms可以ping,但不能卷曲/运行烧瓶应用程序

时间:2018-06-11 18:12:54

标签: flask timeout vagrant virtual-machine private-network

我无法在同一本地网络上运行ubuntu-16.04-i386两个流浪的vms来执行除ping之外的任何操作。最后,我希望能够在两台机器之间使用烧瓶。我正在使用代理,我在SO上使用了非常受欢迎的Proxy with urllib2解决方案。

/ send方法如下所示:

@app.route('/send')
def send_get():
  proxy = urllib2.ProxyHandler({'http': 'my.proxy'})
  opener = urllib2.build_opener(proxy)
  urllib2.install_opener(opener)
  # response = urllib2.urlopen('http://google.com')
  # response = urllib2.urlopen('http://192.168.0.3:5003/receive')
  return response.read() 

我用

运行这个应用程序
if __name__ == '__main__':
  app.run(debug='True', host='0.0.0.0', port=int("5004"))

然后在浏览器中转到http://localhost:5004/send。如果我取消注释上面注释掉的行,我的浏览器会显示一个简单的谷歌主页,这很棒。相反,如果我取消注释它下面的行,我会超时。 192.168.0.3是其他虚拟机的本地IP地址。有一次,send方法看起来像这样:

print('step 1')
req = urllib2.Request('http://192.168.0.3:5003/receive')
print('step 1.5')
print(req) 
print('step 1.5.2')
print(req.get_method())
print('step 2')
response = urllib2.urlopen(req)
print('step 3')
return response.read()

此时控制台将通过step 2打印出来然后吐出以下错误跟踪(我觉得难以辨认)。

10.0.2.2 - - [11/Jun/2018 17:51:01] "GET /send HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/home/vagrant/.local/lib/python2.7/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/vagrant/.local/lib/python2.7/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/home/vagrant/.local/lib/python2.7/site-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/vagrant/.local/lib/python2.7/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/vagrant/.local/lib/python2.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/vagrant/.local/lib/python2.7/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/vagrant/.local/lib/python2.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/vagrant/.local/lib/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/vagrant/alice/send.py", line 17, in send_get
    response = urllib2.urlopen('http://192.168.0.3:5003/receive')
  File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 435, in open
    response = meth(req, response)
  File "/usr/lib/python2.7/urllib2.py", line 548, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 473, in error
    return self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 407, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 556, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 504: Gateway Timeout
10.0.2.2 - - [11/Jun/2018 17:51:01] "GET /send?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
10.0.2.2 - - [11/Jun/2018 17:51:01] "GET /send?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 -
10.0.2.2 - - [11/Jun/2018 17:51:01] "GET /send?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
10.0.2.2 - - [11/Jun/2018 17:51:02] "GET /send?__debugger__=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1" 200 -
10.0.2.2 - - [11/Jun/2018 17:51:02] "GET /send?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -

当我通过http://localhost:5003/receive访问时,接收方法效果很好,所以我不认为这是问题所在,但无论如何都是这样。

@app.route('/receive')
def send_text():
  print('connected')
  return "Hello from Bob's /receive route!"

if __name__ == '__main__':
  app.run(debug='True', host='0.0.0.0', port=int("5003"))

我已经尝试了所有感觉 - 关闭了vms' ufwselinux如果是防火墙,请在未转发的端口中运行/ receive方法,并合并来自Allow two or more vagrant VMs to communicate on their own networkhttps://manski.net/2016/09/vagrant-multi-machine-tutorial/的解决方案而我只想让它发挥作用。目标是在端口5004的alice机器上运行send.py,在端口5003的bob机器上运行receive.py,在浏览器中输入localhost:5004 / send,将该端口转发到发送vm alice上的那个端口运行烧瓶方法/发送,将其伸出到vm bob并获取烧瓶方法/接收的内容,然后将其显示在浏览器中。

Vagrantfile在这里:

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
    config.vm.define "alice" do |alice|
  alice.vm.box = "bento/ubuntu-16.04-i386"
  alice.vm.box_version = "= 2.3.5"
  alice.vm.network "forwarded_port", guest: 8004, host: 8004, host_ip: "127.0.0.1"
  alice.vm.network "forwarded_port", guest: 8084, host: 8084, host_ip: "127.0.0.1"
  alice.vm.network "forwarded_port", guest: 5004, host: 5004, host_ip: "127.0.0.1"
  alice.proxy.http = "http://my.proxy:80"
  alice.proxy.https = "http://my.proxy:80"
  alice.vm.hostname = "alice"

  # Work around disconnected virtual network cable.
  alice.vm.provider "virtualbox" do |vb|
    vb.customize ["modifyvm", :id, "--cableconnected1", "on"]
  end

  alice.vm.network :private_network, ip: "192.168.0.4"

  alice.vm.provision "shell", inline: <<-SHELL
    apt-get install -y avahi-daemon libnss-mdns

    apt-get -qqy update

    # Work around https://github.com/chef/bento/issues/661
    # apt-get -qqy upgrade
    DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade

    apt-get -qqy install make zip unzip postgresql

    apt-get -qqy install python python-pip
    pip2 install --upgrade pip
    pip2 install flask packaging oauth2client redis passlib flask-httpauth
    pip2 install sqlalchemy flask-sqlalchemy psycopg2 bleach requests

    su postgres -c 'createuser -dRS vagrant'
    su vagrant -c 'createdb'
    su vagrant -c 'createdb news'
    su vagrant -c 'createdb forum'
    su vagrant -c 'psql forum -f /vagrant/forum/forum.sql'

    vagrantTip="[35m[1mThe shared directory is located at /vagrant\\nTo access your shared files: cd /vagrant[m"
    echo -e $vagrantTip > /etc/motd

    wget http://download.redis.io/redis-stable.tar.gz
    tar xvzf redis-stable.tar.gz
    cd redis-stable
    make
    make install

    echo "Done installing your virtual machine!"
  SHELL

end

config.vm.define "bob" do |bob|
  bob.vm.box = "bento/ubuntu-16.04-i386"
  bob.vm.box_version = "= 2.3.5"
  bob.vm.network "forwarded_port", guest: 8003, host: 8003, host_ip: "127.0.0.1"
  bob.vm.network "forwarded_port", guest: 8083, host: 8083, host_ip: "127.0.0.1"
  bob.vm.network "forwarded_port", guest: 5003, host: 5003, host_ip: "127.0.0.1"
  bob.proxy.http = "http://my.proxy:80"
  bob.proxy.https = "http://my.proxy:80"
  bob.vm.hostname = "bob"

  # Work around disconnected virtual network cable.
  bob.vm.provider "virtualbox" do |vb|
    vb.customize ["modifyvm", :id, "--cableconnected1", "on"]
  end

  bob.vm.network :private_network, ip: "192.168.0.3"

  bob.vm.provision "shell", inline: <<-SHELL
    apt-get install -y avahi-daemon libnss-mdns

    apt-get -qqy update

    # Work around https://github.com/chef/bento/issues/661
    # apt-get -qqy upgrade
    DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade

    apt-get -qqy install make zip unzip postgresql

    apt-get -qqy install python python-pip
    pip2 install --upgrade pip
    pip2 install flask packaging oauth2client redis passlib flask-httpauth
    pip2 install sqlalchemy flask-sqlalchemy psycopg2 bleach requests

    su postgres -c 'createuser -dRS vagrant'
    su vagrant -c 'createdb'
    su vagrant -c 'createdb news'
    su vagrant -c 'createdb forum'
    su vagrant -c 'psql forum -f /vagrant/forum/forum.sql'

    vagrantTip="[35m[1mThe shared directory is located at /vagrant\\nTo access your shared files: cd /vagrant[m"
    echo -e $vagrantTip > /etc/motd

    wget http://download.redis.io/redis-stable.tar.gz
    tar xvzf redis-stable.tar.gz
    cd redis-stable
    make
    make install

    echo "Done installing your virtual machine!"
  SHELL

end
end

0 个答案:

没有答案