1

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.

Community
  • 1
  • 1
user2908493
  • 11
  • 1
  • 2
  • maybe you have to configure your firewall or enable [port forwarding](https://en.wikipedia.org/wiki/Port_forwarding) on your router... – Filipe Amaral May 17 '16 at 14:43
  • Are you connecting to the local network IP? i.e. 192.168... 172... 10...??? I can see you have omitted it from the question makes me think you are using external IP of the router. – Joe Doherty May 17 '16 at 15:18
  • I have configured my firewall on my Mac to allow connections to port 5000. I have not tried port forwarding yet. I am connecting using http://192.168.x.x using the IP next to inet when I run ifconfig – user2908493 May 17 '16 at 16:00
  • is there anything else running on that port? Try changing the port by setting `app.run(host='0.0.0.0', port=8082)` – Busturdust May 17 '16 at 21:17
  • my workaround was `app.run(host='my_ip_addr',port=5000)` – shivsn May 18 '16 at 06:52
  • 1
    The development server can only [handle one client at a time](http://stackoverflow.com/a/14815932/2800058). – pjcunningham May 18 '16 at 09:00

1 Answers1

2

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.

Community
  • 1
  • 1
brennan
  • 3,392
  • 24
  • 42