Connecting to Flask server from different devices on network results in timeout

user2908493 picture user2908493 · May 17, 2016 · Viewed 9.3k times · Source

I'm currently learning Flask and I decided to try to connect to a very simple server from other devices on my network. I followed the advice given at Flask - configure dev server to be visible across the network and changed

app.run()

to

app.run(host='0.0.0.0')

However, it does not work correctly.

I have a Flask server setup as follows:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return 'Hey there'

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

When I start the server this is the output:

Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

The server works fine when I connect via the localhost however, I always get a timeout when I try to connect from another device on the same network using:

http://<my_ip_address>:5000 

I have tried connecting to the server (which is running on my Macbook Air) from both my Windows 7 Desktop and my iPhone, with both of them receiving timeouts.

Any help regarding this matter would be greatly appreciated.

Answer

brennan picture brennan · Apr 14, 2017

To handle requests concurrently you can run Flask with:

app.run(threaded=True)

By default Flask runs with one thread so subsequent requests are blocked until the thread becomes available. In production, you'll want a WSGI container like Gunicorn to manage workers and threads.