Explain Codes LogoExplain Codes Logo

Configure Flask dev server to be visible across the network

python
flask
networking
firewall
Anton ShumikhinbyAnton Shumikhin·Aug 7, 2024
TLDR

For network-wide accessibility of a Flask app, explicitly run the server with host='0.0.0.0':

# Running the dev server like a boss app.run(host='0.0.0.0')

Verify that firewall rules allow external communication on Flask's default port, 5000, and Don’t Forget to Replace <Your-IP> with Your Actual IP 🏹.

If running from a command line, use:

# Run, Forrest, run... on all ip addresses flask run --host=0.0.0.0

Direct code configuration inside the Flask app also works:

# Why limit to one when you can serve them all? if __name__ == '__main__': app.run(host='0.0.0.0', port=5000)

Remember to locate your IPv4 for sharing access within a network.

Unraveling the network puzzle

Network disruptions can alter local IPs, so keep a lookout for changes:

  1. Check the IPv4 for modifications when the server seems unreachable.
  2. Watch out for an OSError: [WinError 10013], the shout of an access error.
  3. Any port blocking the radio? Use netstat -na|findstr <port> to inspect port usage.

About those walls and other dangers

The Flask development server isn’t ready for life in the wild - note that your firewall settings may save you from malicious bugs 🐛:

  • Ensure you're Open Sesame with firewall rules, permit traffic from your Flask's magic carpet ride, er... port.
  • Remember, your Flask Genie isn't ready for the whole new world - don't use it in production.

Running Flask – persistently

(in Dory's voice) Just keep running, running... 🐠. To keep Flask running even when you quit your terminal:

# Running away from Nemo... nohup python3 app.py &

For Windows users, the equivalent start /b, or perhaps Task Scheduler, is the magic spell.

Final advice, young padawan

While this journey makes your app network-ready, remember Dynamo — not the magician but the production environment — await your Flask app. Be sure to adopt reliable solutions, keep your dependencies updated, and securely manage environment variables.