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.
When I googled "Flask can't be published externally", there are many people who are stuck for the following reasons.
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')
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.
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.
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!
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