Search code examples
flaskip-address

How to restrict flask app to be accessible only on (home) internal network


I want to confirm how to run my flask app so that it is visible/listens on all IPs in my internal (home) network. I do not want it to be accessible externally (i.e. outside of the home network / over the internet).

Following the instructions on "Externally Visible Server" on the Quickstart page,

I can try:

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=1234)

Is that correct, or is the IP "0.0.0.0" making it listen externally (i.e. outside of my home network, as well, by default)?. What is the best way to restrict the app to only listen on the internal (home) network?

Thank you for your help. (I'm not so familiar with the networking side of things.)


Solution

  • Listening on 0.0.0.0 will make your app visible on all your local networks. However, unless you configure port forwarding on your router, your app will be unreachable from outside the network (e.g. across the internet).

    To make sure your app runs on a specific network instead, you can set a specific IP address:

    if __name__ == '__main__':
        app.run(host="192.168.1.5", port=1234)