Flask - Cannot Connect on port 5000

Connection Refused

When you start your Flask app you might find when trying to connect from a remote machine that it doesn’t work. The first thing is to check firewalls, both locally and further out e.g. AWS.

Perhaps when using nginx as a reverse proxy you might see the following error:

[error] 9#0: *47 connect() failed (111: Connection refused) while connecting to upstream, client: xxx.xxx.xx.xxx, server: example.org, request: "POST /my_endpoint HTTP/1.1", upstream: "http://172.18.0.2:5000/my_endpoint", host: "yy.yy.yy.yy"

Solution

If that all seems fine, then you might need to specify the host argument to the app:

python my_app.py --host=0.0.0.0

or, just add it to your code:

if __name__ == '__main__':
    app.run(host='0.0.0.0', threaded=True, debug=True)

Hopefully that helps. If not, let me know