profile
viewpoint

Ask questionsPTVSD port not opend when FLASK use debug mode on.

Environment data

  • PTVSD version: 4.2.10
  • OS and version: "python:3.6.5-slim-stretch" container in docker (Debian 9, 4.9.125-linuxkit)
  • Python version (& distribution if applicable, e.g. Anaconda): 3.6.5
  • Using VS Code or Visual Studio: VS Code 1.35.1

Actual behavior

PTVSD port is opend.

Expected behavior

PTVSD port is not opend.

Steps to reproduce:

  1. Make the sample flask code.
  • In this case, I get the sample code as "remote-debugging-flask" in "vscode-python-sample"
    • URL : https://github.com/DonJayamanne/vscode-python-samples.git
  1. Fix the some configuration and code.
  • app.py
import ptvsd
ptvsd.enable_attach(address=('0.0.0.0', 3500))

from flask import Flask
app = Flask(__name__)

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

if __name__ == '__main__':
    app.run()
  • .env
FLASK_APP="app.py"
FLASK_ENV="development"
FLASK_DEBUG=True
FLASK_RUN_PORT=8000
  1. Run the flask server and check the open port.
# flask run
 * Serving Flask app "app.py" (lazy loading)
 * Environment: development
 * Debug mode: on
 * Running on http://127.0.0.1:8000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 989-143-182

(use another console)
# lsof -i
COMMAND   PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
flask   55916 root    3u  IPv4 6410027      0t0  TCP localhost:8000 (LISTEN)
python  55919 root    3u  IPv4 6410027      0t0  TCP localhost:8000 (LISTEN)
python  55919 root    4u  IPv4 6410027      0t0  TCP localhost:8000 (LISTEN)
  1. Check the PTVSD debug port is not opened.

  2. Change the flask debug mode in env file to false.

  • .env
FLASK_APP="app.py"
FLASK_ENV="development"
FLASK_DEBUG=False
FLASK_RUN_PORT=8000
  1. Run the flask server and check the open port.
# flask run
 * Serving Flask app "app.py"
 * Environment: development
 * Debug mode: off
 * Running on http://127.0.0.1:8000/ (Press CTRL+C to quit)

(use another console)
# lsof -i
COMMAND   PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
flask   55930 root    7u  IPv4 6411880      0t0  TCP *:3500 (LISTEN)
flask   55930 root    9u  IPv4 6411881      0t0  TCP localhost:8000 (LISTEN)
  1. Check the PTVSD debug port is opened.
microsoft/ptvsd

Answer questions kssim

Thank you for your kind and fast answer.

I thinks your answer is maybe works. I went to Step 2 and it worked. But, There is a problem in Step 3. When I attaching Debug in VS Code, Flask is working and I'm getting an error right away.

Error is <code>Failed to attach (connect ECONNREFUSED 127.0.0.1:43320)</code>.

I know what is the problem. Because I used the docker environment, and I need to open the port in container.

I tried to open the exact port, but I can't. Because that port is totally random.

I tried a few time, and I saw the below errors in VS Code.

Failed to attach (connect ECONNREFUSED 127.0.0.1:43320)
Failed to attach (connect ECONNREFUSED 127.0.0.1:43119)
Failed to attach (connect ECONNREFUSED 127.0.0.1:34035)

So, if it's not a docker environment then maybe it works.

However, I have to use a docker environment and can not deal with random ports, so I can not solve this problem with that solution.

useful!
source:https://uonfu.com/
Github User Rank List