Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having problems accessing port 5000 in Vagrant

I am trying to teach myself Flask in a Vagrant environment. I understand that Flask runs a 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 tutorial Flask app:

from flask import Flask 
app = Flask(__name__)

@app.route('/hello')
def hello_world():
    return 'Hello world!'

if __name__ == '__main__':
    app.run(debug=True)

Yet when I run python hello.py in my Vagrant environment and subsequently go to 127.0.0.1:5000/hello in Chrome on my desktop, I can't connect.

I don't know nearly enough about networking. What am I missing?

like image 355
verbsintransit Avatar asked Apr 22 '14 21:04

verbsintransit


1 Answers

If you are accessing from Chrome in your desktop, you are technically accessing from a different computer (hence you need to place host='0.0.0.0' as argument to app.run() to tell the guest OS to accept connections from all public (external) IPs.

This is what worked for me (for both 127.0.0.1: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')
like image 161
Joben R. Ilagan Avatar answered Sep 20 '22 00:09

Joben R. Ilagan