我创建了一个无业游民的机器。这是我的Vagrantfile:
Vagrant.configure("2") do |config|
config.vm.box = "bento/ubuntu-18.04"
config.vm.network "forwarded_port", guest: 5000, host: 5000
config.vm.provision "shell", inline: <<-SHELL
sudo apt-get update
sudo apt-get -y install python3-pip
sudo pip3 install flask
SHELL
end
在虚拟机中一切正常。在虚拟机中运行以下应用程序可以正常工作。 basic.py:
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/')
def index():
return jsonify({"message": "Hello World"})
if __name__ == '__main__':
app.run(debug=True, port=5000)
Result of running basic.py inside the virtual machine
但是尝试从主机(通过Chrome浏览器)访问服务器会显示以下消息: unable to access the server from the host machine
我试图禁用防病毒软件(Norton只是为了检查Norton是否正在过滤和阻止端口),但结果却相同。还有其他人遇到过同样的问题吗?有人可以帮助您找出问题所在,然后解决该问题吗?
@Mirko Ebert,尝试通过“ curl -v localhost:5000”访问服务器将给出以下响应: 从虚拟机内部:
vagrant@vagrant:~$ curl -v localhost:5000
* Rebuilt URL to: localhost:5000/
* Trying ::1...
* TCP_NODELAY set
* connect to ::1 port 5000 failed: Connection refused
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 5000 (#0)
> GET / HTTP/1.1
> Host: localhost:5000
> User-Agent: curl/7.58.0
> Accept: */*
>
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Content-Type: application/json
< Content-Length: 31
< Server: Werkzeug/0.14.1 Python/3.6.7
< Date: Thu, 13 Dec 2018 21:15:45 GMT
<
{
"message": "Hello World"
}
* Closing connection 0
在主机上:
$ curl -v localhost:5000
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0*
Trying ::1...
* TCP_NODELAY set
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 5000 (#0)
> GET / HTTP/1.1
> Host: localhost:5000
> User-Agent: curl/7.62.0
> Accept: */*
>
* Recv failure: Connection was reset
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
* Closing connection 0
curl: (56) Recv failure: Connection was reset