iJade
iJade

Reputation: 23801

Access web server running on Virtual box ubuntu on host windows machine

I'm running Ubuntu using Virtual Box Manager from windows machine. Inside the VM box ubuntu i'm running a python flask application which is running at http://localhost:5000.

I tried to access the VM box localhost URL on windows machine using the VM box IP which I got using ifconfig. But it's says :

Your Internet access is blocked

Am i accessing it the right way ?

here is my python flask code :

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

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

Upvotes: 1

Views: 3413

Answers (2)

Prashant Shetty
Prashant Shetty

Reputation: 23

Below code will allow you to access Flask web from any public IP instead of 127.0.0.1

if __name__ == '__main__':
   app.run(host='0.0.0.0', debug='TRUE')

By default Flask runs on port: 5000. Sometime on VM this port will be blocked. To allow traffic on this port execute below command.

iptables -I INPUT -p tcp --dport 5000 -j ACCEPT

Upvotes: 1

Hassan Mehmood
Hassan Mehmood

Reputation: 1402

You need to specify a host='0.0.0.0' while starting your app. By default it will only accept requests from localhost. So if you are sending a request from some other IP then you must have to specify a host.

See below example.

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run(host='0.0.0.0')

Also if you want to activate the debugging mode to analyse the exceptions/errors while you access your application. You can also set debug attribute to 'True'.

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

Upvotes: 5

Related Questions