Configure Flask dev server to be visible across the network

前端 未结 14 2750
面向向阳花
面向向阳花 2020-11-21 05:57

I\'m not sure if this is Flask specific, but when I run an app in dev mode (http://localhost:5000), I cannot access it from other machines on the network (with

14条回答
  •  佛祖请我去吃肉
    2020-11-21 06:57

    While this is possible, you should not use the Flask dev server in production. The Flask dev server is not designed to be particularly secure, stable, or efficient. See the docs on deploying for correct solutions.


    Add a parameter to your app.run(). By default it runs on localhost, change it to app.run(host= '0.0.0.0') to run on all your machine's IP addresses. 0.0.0.0 is a special value, you'll need to navigate to the actual IP address.

    Documented on the Flask site under "Externally Visible Server" on the Quickstart page:

    Externally Visible Server

    If you run the server you will notice that the server is only available from your own computer, not from any other in the network. This is the default because in debugging mode a user of the application can execute arbitrary Python code on your computer. If you have debug disabled or trust the users on your network, you can make the server publicly available.

    Just change the call of the run() method to look like this:

    app.run(host='0.0.0.0')

    This tells your operating system to listen on a public IP.

提交回复
热议问题