一分钟带你速通vscode调试python web —— 快速配置debugpy

这个比xdebug好配多了,只需要两三步就可以完成。

首先拿出我们需要调试的代码

from flask import Flask, request, render_template_string

app = Flask(__name__)

@app.route('/')
def index():
    # 1. 获取 URL 参数中的 'code'
    # 例如访问: http://127.0.0.1:5000/?code={{7*7}}
    code = request.args.get('code', 'Guest')
    
    # 2. [致命漏洞] 直接将用户输入拼接到模板字符串中
    # 正常开发应该使用 render_template('file.html', code=code)
    # 而不是下面这种拼接字符串的方式
    html = '''
    <h3>Hello, %s !</h3>
    <p>这是一个用来测试内存马的 SSTI 漏洞靶场。</p>
    ''' % code
    
    # 3. 渲染这个包含用户输入的字符串
    # 如果用户输入的是 {{ config }},Jinja2 引擎就会执行它
    return render_template_string(html)

if __name__ == '__main__':
    # 启动服务
    app.run(host='0.0.0.0', port=5000, debug=True, use_reloader=False)

首先我们需要安装python 和 python debug扩展,然后我们需要安装debugpy

pip install debugpy

当你按下 F5 启动调试时,VS Code 实际上执行了类似这样的操作:

并不是: python app.py 而是: python -m debugpy app.py

所以app.py运行端口和debugpy端口无需保持一致。

我们需要在需要执行的代码前面加上

# 导入 debugpy 库,debugpy 是一个用于在 Python 中进行调试的库,通常与 Visual Studio Code 配合使用
import debugpy
try:
    # 调用 debugpy 的 listen 方法,使调试器开始监听指定的主机和端口。在这里,监听的主机是 'localhost',端口是 9501。默认情况下,VS Code 调试配置会使用 5678 端口,但这里使用了 9501。
    debugpy.listen(("localhost", 9501))
     # 输出信息,提示用户调试器正在等待附加连接
    print("Waiting for debugger attach")
    # 等待调试器(例如 VS Code)连接到当前 Python 进程。程序会在这一行暂停,直到调试器附加进来。
    debugpy.wait_for_client()
# 捕获所有异常,若有异常发生,进入 except 块
except Exception as e:
    # 如果发生异常,则什么也不做,直接跳过
    pass

from flask import Flask, request, render_template_string
# 导入 debugpy 库,debugpy 是一个用于在 Python 中进行调试的库,通常与 Visual Studio Code 配合使用
import debugpy
try:
    # 调用 debugpy 的 listen 方法,使调试器开始监听指定的主机和端口。在这里,监听的主机是 'localhost',端口是 9501。默认情况下,VS Code 调试配置会使用 5678 端口,但这里使用了 9501。
    debugpy.listen(("localhost", 9501))
     # 输出信息,提示用户调试器正在等待附加连接
    print("Waiting for debugger attach")
    # 等待调试器(例如 VS Code)连接到当前 Python 进程。程序会在这一行暂停,直到调试器附加进来。
    debugpy.wait_for_client()
# 捕获所有异常,若有异常发生,进入 except 块
except Exception as e:
    # 如果发生异常,则什么也不做,直接跳过
    pass

app = Flask(__name__)

@app.route('/')
def index():
    # 1. 获取 URL 参数中的 'code'
    # 例如访问: http://127.0.0.1:5000/?code={{7*7}}
    code = request.args.get('code', 'Guest')
    
    # 2. [致命漏洞] 直接将用户输入拼接到模板字符串中
    # 正常开发应该使用 render_template('file.html', code=code)
    # 而不是下面这种拼接字符串的方式
    html = '''
    <h3>Hello, %s !</h3>
    <p>这是一个用来测试内存马的 SSTI 漏洞靶场。</p>
    ''' % code
    
    # 3. 渲染这个包含用户输入的字符串
    # 如果用户输入的是 {{ config }},Jinja2 引擎就会执行它
    return render_template_string(html)

if __name__ == '__main__':
    # 启动服务
    app.run(host='0.0.0.0', port=5000, debug=True, use_reloader=False)

同时launch.json

{
    // 使用 IntelliSense 了解相关属性
    // 悬停以查看现有属性的描述
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python 调试程序: 当前文件",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
        },

        {
            "name": "sh_file_debug",
            "type": "debugpy",
            "request": "attach",
            "connect": {
                "host": "localhost",
                "port": 9501
            },
        }
    ]
}

launch.json的端口需要和加入的那段代码中的端口保持一致。

如果要跟进系统函数,则加上一条

{
    // 使用 IntelliSense 了解相关属性
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python 调试程序: 当前文件",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": false // 不只调试当前代码
        },

        {
            "name": "sh_file_debug",
            "type": "debugpy",
            "request": "attach",
            "connect": {
                "host": "localhost",
                "port": 9501
            },
        }
    ]
}
posted @ 2025-11-28 20:35  Acc1oFl4g  阅读(13)  评论(0)    收藏  举报