Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure Flask 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?

like image 854
sa125 Avatar asked Aug 11 '11 08:08

sa125


People also ask

How do I make a flask available on a network?

Today, there are two ways to expose your flask application to the internet. Deploy the web application in your office server which has a public IP address and domain name. Deploy the web application in the cloud such as AWS, MS Azure, GCP or web hosting companies like GoDaddy, SiteGround, A2Hosting etc.

How do I run a flask on another IP?

run(host= '0.0. 0.0') to run on your machines IP address. Another thing you can do is use the flask executable to start your server, you can use flask run --host=0.0. 0.0 to change the default IP which is 127.0.

How do I host my flask app on localhost?

To install flask, simply type in pip install flask in your computer terminal/command line. Once you have made sure flask is installed, simply run the hello.py script. Once you run the script, the website should now be up and running on your local machine, and it can be viewed by visiting localhost:5000 in your browser.


2 Answers

While this is possible, you should not use the Flask dev server in production. The Flask dev server is not designed to be particularly secure, stable, or efficient. See the docs on deploying for correct solutions.


The --host option to flask run, or the host parameter to app.run(), controls what address the development server listens to. By default it runs on localhost, change it to flask run --host=0.0.0.0 (or app.run(host="0.0.0.0")) to run on all your machine's IP addresses.

0.0.0.0 is a special value that you can't use in the browser directly, you'll need to navigate to the actual IP address of the machine on the network. You may also need to adjust your firewall to allow external access to the port.

The Flask quickstart docs explain this in the "Externally Visible Server" section:

If you run the server you will notice that the server is only accessible 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 the debugger disabled or trust the users on your network, you can make the server publicly available simply by adding --host=0.0.0.0 to the command line:

$ flask run --host=0.0.0.0 

This tells your operating system to listen on all public IPs.

like image 155
Shawn Swaner Avatar answered Oct 06 '22 19:10

Shawn Swaner


If you use the flask executable to start your server, use flask run --host=0.0.0.0 to change the default from 127.0.0.1 and open it up to non-local connections.

If you run the server you will notice that the server is only accessible 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 the debugger disabled or trust the users on your network, you can make the server publicly available simply by adding --host=0.0.0.0 to the command line:

$ flask run --host=0.0.0.0 

This tells your operating system to listen on all public IPs.

Reference: https://flask.palletsprojects.com/quickstart/

like image 45
Aria Avatar answered Oct 06 '22 20:10

Aria