Set host-ip list in python-flask server

23 views Asked by At

I would like to set a list of hosts (IPs) on which my Flask server can listen on so that if the main Ethernet port blows up there is the other one that can support the traffic (kind of simple failover).
In my case I have a NIC with 4 ports and only 2 of them can be used by flask, the others must stay private (therefore I cannot set host='0.0.0.0')

For example this is the current configuration:

...
app = Flask(__name__, instance_relative_config=True)
app.run(debug=False, host='192.168.1.10', port=5000)

and I would like to get the following:

...
app = Flask(__name__, instance_relative_config=True)
app.run(debug=False, host='192.168.1.10,192.168.1.11', port=5000)

Do you have any idea?

Many thanks in advance

1

There are 1 answers

0
larsks On

Flask's built-in server isn't supposed to be used for anything other than development work:

“Production” means “not development”, which applies whether you’re serving your application publicly to millions of users or privately / locally to a single user. Do not use the development server when deploying to production. It is intended for use only during local development. It is not designed to be particularly secure, stable, or efficient. (docs)

To get the behavior you want, use a more robust WSGI server like gunicorn, which would allow you to listen on multiple addresses like this (assuming your code lives in app.py):

gunicorn app:app --bind 192.168.1.10:5000 --bind 192.168.1.11:5000