从 visual studio 代码自动启动 python 网络服务器

Start automatically python webserver from visual studio code

我想在 visual studio 代码中按下 运行 按钮(或 F5)时自动启动 Python http.server。 我认为这是 launch.json 配置的问题,但我不熟悉它。 我该怎么做?

请安装 pythonVSCode 扩展。并在您的项目目录中创建 python 文件。并将以下内容放在上面。参考 here

import http.server
import socketserver

PORT = 8000

Handler = http.server.SimpleHTTPRequestHandler

with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print("serving at port", PORT)
    httpd.serve_forever()

并像这样创建启动配置...

{
    "name": "Documentation",
    "type": "python",
    "request": "launch",
    "stopOnEntry": false,
    "pythonPath": "${workspaceRoot}/env/bin/python",
    "program": "${workspaceRoot}/your_pyton_run_file.py",
    "debugOptions": [
        "WaitOnAbnormalExit",
        "WaitOnNormalExit",
        "RedirectOutput"
    ]
},

然后你可以按F5开始这个