想用通常的办法调试上面两个家伙是行不通的,在看到mitmproxy的官方文档后,经过实验,有了以下结果,基本是原创吧,全网都找不到,你有梯子去外面都找不到
注意:两种调试工具(pydevd-charm与ptvsd)可能会有冲突(但是我测试的时候没有发生)
途径1:pycharm
调试工具:pydevd_pycharm
注意:这里必须要用到pycharm的专业版,社区版没有调试服务器的功能
1.使用pycharm打开项目文件夹,
2.配置python debug server
Run->Edit Configurations,打开Run/Debug Configurations对话框,点+号,选择Python Debug Server
配置好host地址和端口,其他的默认即可
3.连接
在需要调试的文件中加入如下代码,
import pydevd_pycharm
pydevd_pycharm.settrace('localhost', port=5678, stdoutToServer=True, stderrToServer=True) #连接调试服务器并挂起
当程序运行到这两行代码时,就会尝试连接Python Debug Server,并挂起程序,所以通常的做法是在程序的开头加入如上代码
4.挂起
在需要断点的地方调用一次pydevd_pycharm.settrace()
,后面的代码就可以正常断点了,
途径2:vscode
调试工具:ptvsd
https://github.com/microsoft/ptvsd
1.使用vscode打开项目文件夹,
2.配置
在launch.json中添加一个配置
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: 当前文件",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
},
{
"name": "Attach (Remote Debug)",
"type": "python",
"request": "attach",
"pathMappings":[
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "."
}
],
"port": 5678,
"host": "localhost"
}
]
}
“Attach (Remote Debug)”部分就是用于远程调试的配置,端口(port)按需要进行调整
3.连接
在需要调试的文件开头添加如下代码,地址和端口要和上面一样
import ptvsd
ptvsd.enable_attach(address =('localhost',5678))
ptvsd.wait_for_attach()
4.挂起
在需要断点的地方调用一次breakpoint()
或者ptvsd.break_into_debugger()
,后面的代码就可以正常断点了,
def handleRequest(session):
# session.bBufferResponse = True
# session.utilSetRequestBody(oBody)
print("request")
a=1
# pydevd_pycharm.settrace()
breakpoint()
b=2
ptvsd.break_into_debugger()#和 breakpoint()效果一样
c=3
fiddlercore和mitmproxy的区别
正常情况下,只要连接上远程调试器,就可以正常打断点进行调试,mitmproxy就属于这种情况,但是fiddlercore不一样,fiddlercore的代码实际上是分成两个部分的,一是fiddlercore的配置,二是事件响应,
# register event handler
# object.SomeEvent += handler
#
# unregister event handler
# object.SomeEvent -= handler
#
# passed a callable Python object to get a delegate instance.
FC.FiddlerApplication.Log.OnLogString += printLog
FC.FiddlerApplication.BeforeRequest += handleRequest
FC.FiddlerApplication.BeforeResponse += handleResponse
# When decrypting HTTPS traffic,ignore the server certificate errors
FC.CONFIG.IgnoreServerCertErrors = False
iPort=9977
startupSettings = FiddlerCoreStartupSettingsBuilder().ListenOnPort(iPort).DecryptSSL().OptimizeThreadPool().Build()
# start up capture
FC.FiddlerApplication.Startup(startupSettings)
上面代码输入配置和启动fiddlecore的代码
def handleRequest(session):
# session.bBufferResponse = True
# session.utilSetRequestBody(oBody)
print("request")
a=1
b=2
c=3
pass
上面代码就是一个事件响应函数,当某个事件发生的时候会被触发,相当于一个新的程序(到底是线程还是进程还是啥我也不知道,只知道在c#中是一个委托),要在这些事件响应函数中进行调试,就要在函数开头使用挂起操作