Flask Web server cannot be published to the outside [VScode]

Overview

When I debug-start a simple Web server built with Flask from VScode, I can access it locally without any problem, but I cannot access it from the outside.

Start debugging the following run.py by pressing F5 on VScode

run.py


from flask import Flask, make_response, jsonify
app = Flask(__name__)

@app.route('/')
def hello_world():
    return "Hello, World!"

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

to start

 * Serving Flask app "flask/run.py"
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

I can access it locally without any problems

$ curl http://localhost:5000/
Hello, World!

Cannot be accessed from outside

$ curl http://192.168.xxx.yyy:5000/
curl: (7) Failed to connect to 192.168.206.129 port 80: Connection refused

I'm wondering if some people get stuck in the same place, so I'll post it.

Most causes

When I googled "Flask can't be published externally", there are many people who are stuck for the following reasons.

0.0.0.0 is not specified for host

Flask does not allow external disclosure by default. Therefore, if you want to publish to the outside, you need to explicitly specify 0.0.0.0 as shown below.

run.py


from flask import Flask, make_response, jsonify
app = Flask(__name__)

@app.route('/')
def hello_world():
    return "Hello, World!"

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

The port is not open

If the above 0.0.0.0 doesn't help, it's usually this. You can open it to the outside by disabling the firewall or opening the port you want to use.

Cause of this time

Even if you try the above two solutions, if you haven't solved it yet, you may be stumbling in the same place as me.

launch.json was wrong

When you start F5 with VScode, the settings in launch.json will be loaded. Based on this, VScode will start the program by hitting the start command nicely. My launch.json that was automatically generated this time was as follows.

launch.json


{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Flask",
            "type": "python",
            "request": "launch",
            "module": "flask",
            "env": {
                "FLASK_APP": "flask/run.py"
            },
            "args": [
                "run",
                "--no-debugger",
                "--no-reload"
            ],
            "jinja": true
        }
    ]
}

The command generated from this launch.json is flask run --no-debugger --no-reload I feel like this.

Apparently, it was necessary to specify host = '0.0.0.0' in this part. Added host and port specifications to args in launch.json

launch.json


{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Flask",
            "type": "python",
            "request": "launch",
            "module": "flask",
            "env": {
                "FLASK_APP": "flask/run.py"
            },
            "args": [
                "run",
                "--no-debugger",
                "--no-reload",
                "--host=0.0.0.0",
                "--port=5000"
            ],
            "jinja": true
        }
    ]
}

It is now possible to open it to the outside.

$ curl http://localhost:5000/
Hello, World!

Reflection

When I started it with F5, the command that VScode executed is output properly If you look here from the beginning, you may have noticed earlier

#On the first line-m flask run --no-debugger --no-reload is written properly
$ /home/user/.local/share/virtualenvs/flask-FhE9rMZ_/bin/python /home/user/.vscode/extensions/ms-python.python-2019.10.44104/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 34251 -m flask run --no-debugger --no-reload 
 * Serving Flask app "flask/run.py"
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

You have to look closely at the logs ...

Recommended Posts

Flask Web server cannot be published to the outside [VScode]
What to do if the package dependency cannot be repaired
[Flask] I tried to summarize the "docker-compose configuration" that can be created quickly for web applications
Run the output code on the local web server as "A, pretending to be B" in python
Settings to be made when starting up the linux server centos7
Supplement to the explanation of vscode
The easiest way to make Flask
I tried to push the Sphinx document to BitBucket and it will be automatically reflected on the web server
Try using the Python web framework Django (1)-From installation to server startup
Settings that allow IPython Notebook to be accessed from outside the local
Address to the bug that node.surface cannot be obtained with python3 + mecab
Start the web server in the current directory
Try using the web application framework Flask
How to access the Datastore from the outside
Publish the current directory on the web server
A solution to the problem that the Python version in Conda cannot be changed
[Flask + Keras] How to infer multiple models at high speed on the server