sa125 sa125 - 1 month ago 25
Python Question

Flask - configure dev server to be visible across the network

I'm not sure if this is Flask specific, but when I run an app in dev mode (

http://localhost:5000
), I cannot access it from other machines on the network (with
http://[dev-host-ip]:5000
). With Rails in dev mode, for example, it works fine. I couldn't find any docs regarding the Flask dev server configuration. Any idea what should be configured to enable this?

Answer

Add a parameter to your app.run(). By default it runs on localhost, change it to app.run(host= '0.0.0.0') to run on your machines IP address.

Documented on the Flask site under "Externally Visible Server" on the Quickstart page:

Externally Visible Server

If you run the server you will notice that the server is only available from your own computer, not from any other in the network. This is the default because in debugging mode a user of the application can execute arbitrary Python code on your computer. If you have debug disabled or trust the users on your network, you can make the server publicly available.

Just change the call of the run() method to look like this:

app.run(host='0.0.0.0')

This tells your operating system to listen on a public IP.