0

I installed Vagrant in windows, now I have a virtual ubuntu , I run a python script :

vagrant@precise32:/vagrant/FlaskMysql/FlaskApp$ ls
app.py  static  storedPro.txt  templates
vagrant@precise32:/vagrant/FlaskMysql/FlaskApp$ python app.py
* Running on http://127.0.0.1:5002/ (Press CTRL+C to quit)

my Vagrantefile :

config.vm.box = "hashicorp/precise32"
  config.vm.provision :shell, path: "bootstrap.sh"
  config.vm.network :forwarded_port, guest: 80, host: 4567
  config.vm.network :forwarded_port, guest: 5002, host: 5002

I tried to access the above address from the browser in my window, the index.html page appears in a couple of seconds then disappears.

UPDATE :

vagrant@precise32:/vagrant/FlaskMysql/FlaskApp$ curl http://127.0.0.1:5000
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your   request.  Either the server is overloaded or
there is an error in the application.</p>

Thanks.

2
  • 1
    your host machine needs to forward 5002 to the guest machines port 5002? Commented Oct 22, 2015 at 13:12
  • I already added : config.vm.network :forwarded_port, guest: 5002, host: 5002 but still getting : This webpage is not available Commented Oct 22, 2015 at 13:37

3 Answers 3

3

In addition to forwarding the port you'll need to run the Flask app with host "0.0.0.0":

app.run(host='0.0.0.0', port=5002)

This makes the dev server externally visible; in the case of Vagrant, we want the app to be externally visible (from the guest OS to the host OS).

Sign up to request clarification or add additional context in comments.

6 Comments

I changed it to app.run(host='0.0.0.0', port=5002) as you said, and when I went to 0.0.0.0:5002 I always get : This webpage is not available
@BouchaibMounir From a browser on your host machine you'll go to http://127.0.0.1:5002 to access the site.
I added an UPDATE in my question, curl.
NOW when I tried to access 127.0.0.1:5000 from the browser in my window, the index.html page appears, when I click in a link it disappears and when I refresh the page it disappears,
@BouchaibMounir you are serving the app on port 5002, not 5000. Either you need to serve the app on port 5000 or you need to update your curl to hit port 5002
|
0

Thanks to chucksmash's answer, this is my version of the solution:

I changed the host and port in the Python App from this:

run(host='localhost', port=8080, debug=True)

to this:

run(host='0.0.0.0', port=5002, debug=True)

and in the vagrantfile from this:

config.vm.network "forwarded_port", guest: 8080, host: 8080, host_ip: "127.0.0.1"

to this:

config.vm.network "forwarded_port", guest: 5002, host: 5002

then test a request and do NOT forget to add the resource (URL endpoint) 'hello' in my example: http://0.0.0.0:5002/hello

this fixed the issue for me.

Comments

0

I want to add more to Chucksmash's answer, if anyone is still facing issues with connecting to flask URL on host machine. Try one of these options (both should work):

Vagrantfile:

config.vm.network "forwarded_port", guest: 5000, host: 5000, host_ip: "127.0.0.1"

app.py:

from flask import Flask

app = Flask(__name__)


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


if __name__ == "__main__":
    app.run()

Method 1. Configure host and port in app.run() and run the flash app using python.

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)

Run flask app:

$ python3 app.py

Method 2. Pass host and port as arguments to flask run:

export FLASK_APP=app.py
flask run --host "0.0.0.0" --port 5000

You can verify if it works by running the following on terminal:

curl http://127.0.0.1:5000

output: Hello world!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.