Configure Flask dev server to be visible across the network

前端 未结 14 2337
面向向阳花
面向向阳花 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:49

    go to project path set FLASK_APP=ABC.py SET FLASK_ENV=development

    flask run -h [yourIP] -p 8080 you will following o/p on CMD:- * Serving Flask app "expirement.py" (lazy loading) * Environment: development * Debug mode: on * Restarting with stat * Debugger is active! * Debugger PIN: 199-519-700 * Running on http://[yourIP]:8080/ (Press CTRL+C to quit)

    0 讨论(0)
  • 2020-11-21 06:51

    Create file .flaskenv in the project root directory.

    The parameters in this file are typically:

    FLASK_APP=app.py
    FLASK_ENV=development
    FLASK_RUN_HOST=[dev-host-ip]
    FLASK_RUN_PORT=5000
    

    If you have a virtual environment, activate it and do a pip install python-dotenv .

    This package is going to use the .flaskenv file, and declarations inside it will be automatically imported across terminal sessions.

    Then you can do flask run

    0 讨论(0)
  • 2020-11-21 06:52

    If you're having troubles accessing your Flask server, deployed using PyCharm, take the following into account:

    PyCharm doesn't run your main .py file directly, so any code in if __name__ == '__main__': won't be executed, and any changes (like app.run(host='0.0.0.0', port=5000)) won't take effect.

    Instead, you should configure the Flask server using Run Configurations, in particular, placing --host 0.0.0.0 --port 5000 into Additional options field.

    More about configuring Flask server in PyCharm

    0 讨论(0)
  • 2020-11-21 06:52

    For me i followed the above answer and modified it a bit:

    1. Just grab your ipv4 address using ipconfig on command prompt
    2. Go to the file in which flask code is present
    3. In main function write app.run(host= 'your ipv4 address')

    Eg:

    0 讨论(0)
  • 2020-11-21 06:53

    This answer is not solely related with flask, but should be applicable for all cannot connect service from another host issue.

    1. use netstat -ano | grep <port> to see if the address is 0.0.0.0 or ::. If it is 127.0.0.1 then it is only for the local requests.
    2. use tcpdump to see if any packet is missing. If it shows obvious imbalance, check routing rules by iptables.

    Today I run my flask app as usual, but I noticed it cannot connect from other server. Then I run netstat -ano | grep <port>, and the local address is :: or 0.0.0.0 (I tried both, and I know 127.0.0.1 only allows connection from the local host). Then I used telnet host port, the result is like connect to .... This is very odd. Then I thought I would better check it with tcpdump -i any port <port> -w w.pcap. And I noticed it is all like this:

    Then by checking iptables --list OUTPUT section, I could see several rules:

    these rules forbid output tcp vital packets in handshaking. By deleting them, the problem is gone.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题