visual-studio-code - 从 Visual Studio Code 自动启动 Python Web 服务器

标签 visual-studio-code simplehttpserver

当我从 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 启动

关于visual-studio-code - 从 Visual Studio Code 自动启动 Python Web 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40179185/

相关文章:

c# - 调试时VSCode Asp.Net核心命令行参数?

angular - 每次我在 .ts 文件中添加断点时,Chrome 都会烦人地在 main.bundle.ts 中打开相应的行

visual-studio-code - 是否可以将给定语言与VSCode中的文件扩展名关联?

python-3.x - 从 Python 的 http.server 提供文件 - 使用文件正确响应

python - PyQt 简单 HTTP 服务器 : GUI freezes on starting server

python - SimpleHTTPServer 和 SocketServer

python - 使用 Python BaseHTTPServer 处理同时/异步请求

visual-studio-code - 如何删除已发布的 vscode 扩展?

typescript - 如何在 VsCode 中禁用自动导入语句 2017 年 10 月

python - 如何在 Python 中将请求重定向到不同的 URL