有没有办法在后台以编程方式启动mitmproxy v.7.0.2?
ProxyConfig和ProxyServer自7.0.0版以来已被删除,下面的代码不起作用
from mitmproxy.options import Options
from mitmproxy.proxy.config import ProxyConfig
from mitmproxy.proxy.server import ProxyServer
from mitmproxy.tools.dump import DumpMaster
import threading
import asyncio
import time
class Addon(object):
def __init__(self):
self.num = 1
def request(self, flow):
flow.request.headers["count"] = str(self.num)
def response(self, flow):
self.num = self.num + 1
flow.response.headers["count"] = str(self.num)
print(self.num)
# see source mitmproxy/master.py for details
def loop_in_thread(loop, m):
asyncio.set_event_loop(loop) # This is the key.
m.run_loop(loop.run_forever)
if __name__ == "__main__":
options = Options(listen_host='0.0.0.0', listen_port=8080, http2=True)
m = DumpMaster(options, with_termlog=False, with_dumper=False)
config = ProxyConfig(options)
m.server = ProxyServer(config)
m.addons.add(Addon())
# run mitmproxy in backgroud, especially integrated with other server
loop = asyncio.get_event_loop()
t = threading.Thread( target=loop_in_thread, args=(loop,m) )
t.start()
# Other servers, such as a web server, might be started then.
time.sleep(20)
print('going to shutdown mitmproxy')
m.shutdown()
from BigSully's gist
Tags:
您可以将
Addon
类放入your_script.py
中,然后运行mitmdump -s your_script.py
。mitmdump没有控制台界面,可以在后台运行我们(mitmproxy开发人员)正式不再支持Python的手动实例化,因为这给我们带来了大量的支持负担。如果您有一些Python经验,您可能会找到自己的方法
如果我的加载项有其他依赖项怎么办?
方法1:
pip install mitmproxy
仍然得到了完美的支持,并为您提供了与独立二进制文件相同的功能。额外提示:您可以运行venv/bin/mitmproxy
或venv/Scripts/mitmproxy.exe
在您的virtualenv中调用mitmproxy,而无需激活您的virtualenv方法2:您可以使用
如何调试mitmproxy本身?pipx
安装mitmproxy,然后运行pipx inject mitmproxy <your dependency name>
。详情见https://docs.mitmproxy.org/stable/overview-installation/#installation-from-the-python-package-index-pypi如果您是从命令行(无论是打印语句还是pdb)进行调试,最简单的方法是运行
mitmdump
而不是mitmproxy
,它提供了与控制台界面相同的功能。或者,您可以使用PyCharm的远程调试功能,该功能在控制台界面处于活动状态(https://github.com/mitmproxy/mitmproxy/blob/main/examples/contrib/remote-debug.py)时也可以工作相关问题 更多 >
编程相关推荐