【问题标题】:Start automatically python webserver from visual studio code从 Visual Studio 代码自动启动 python webserver
【发布时间】:2016-10-21 14:25:15
【问题描述】:

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

【问题讨论】:

    标签: visual-studio-code simplehttpserver


    【解决方案1】:

    请安装 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 开始这个

    【讨论】:

    • 不能给扩展添加链接吗?我刚刚用谷歌搜索了 pythonVSCode 并出现了多个扩展。我认为它们可能并非都按照您建议的方式工作。
    • @Chillie 我最近没有测试过 vscode python 网络服务器。并且 pythonVSCode repo 迁移到了微软。这是链接github.com/Microsoft/vscode-python
    猜你喜欢
    • 1970-01-01
    • 2016-04-08
    • 2017-04-20
    • 2016-11-07
    • 2019-05-26
    • 1970-01-01
    • 2016-06-02
    • 2018-05-27
    • 2023-03-15
    相关资源
    最近更新 更多