Problems accessing port 5000 in Vagrant

I am trying to teach myself to Flyaz in a vagabond environment. I understand that Flask starts the server on port 5000 by default. In my Vagrantfile, I have:

config.vm.network :forwarded_port, guest: 80, host: 8080 config.vm.network :forwarded_port, guest: 5000, host: 5000 

I have a simple Flask application:

 from flask import Flask app = Flask(__name__) @app.route('/hello') def hello_world(): return 'Hello world!' if __name__ == '__main__': app.run(debug=True) 

However, when I run python hello.py in my Vagrant environment and then switch to 127.0.0.1:5000/hello python hello.py in Chrome on my desktop, I cannot connect.

I hardly know what to do with the network. What am I missing?

+6
source share
5 answers

You may need Flask to serve on an external visible URL: see docs

+11
source

If you access Chrome on your desktop, you get technical access from another computer (so you need to put host='0.0.0.0' as an argument in app.run() to tell the guest OS to accept connections from all open (external) IP.

This is what worked for me (for 127.0.0.1:5000/hello localhost:5000/hello and localhost:5000/hello in Chrome):

 from flask import Flask app = Flask(__name__) @app.route("/hello") def hello(): return "Hello World!" if __name__ == "__main__": app.run(host='0.0.0.0') 
+13
source

Do you have a curl set in your stray box? If you do not install it and try curl http://127.0.0.1:5000/hello . If you get an answer and you see Hello world! in your console, then everything will be fine on the flask. Return to the virtual window. When you open the network settings, from what you said above, I assume that you are using a NAT address. In this case, you will need to set the host address 127.0.0.1, port 5000, leave the guest address blank and put port 5000 again, and this should do the trick (port forwarding). One thing I noticed about the wander in these situations is that it works best if you use virtualhosts. Take a look here .

+1
source

This may be because Vagrant NAT (VirtualBox) NAT redirection does not work properly (port conflicts).

To narrow down the problem, you can make sure port 5000 is open at both ends, this can be done using nmap, nc (netcat) or netstat, etc.

eg. on the host

 nmap 127.0.0.1 nc -vz 127.0.0.1 5000 curl http://127.0.0.1:5000 

Away

 nmap GUEST_IP nc -vz GUEST_IP 5000 curl http://GUEST_IP:5000 

NOTE. GUEST_IP is most likely located on a 10.0.2.0/24 network (the NAT NAT module is used by default).

Executing these commands both on your host and inside the virtual machine (guest box) will tell you if the ports are open.

Make sure your python hello world is not ONLY bound to the loopback device so that it can handle requests from external clients.

Use lsof -i :5000 or netstat -nap | grep :5000 netstat -nap | grep :5000 to determine which program is binding the port for further troubleshooting.

+1
source

make sure you restart the firewall after changing your vagrantfile

0
source

All Articles