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()
Run Code Online (Sandbox Code Playgroud)
您可以将您的Addon课程放入your_script.py然后运行mitmdump -s your_script.py。mitmdump 没有控制台界面,可以在后台运行。
我们(mitmproxy 开发者)正式不再支持 Python 的手动实例化,因为这给我们带来了大量的支持负担。如果您有一些 Python 经验,您可能会找到解决方法。
方法 1: pip install mitmproxy仍然得到完美支持,并为您提供与独立二进制文件相同的功能。额外提示:您可以在 virtualenv 中运行venv/bin/mitmproxy或调用 mitmproxy,而无需激活 virtualenv。venv/Scripts/mitmproxy.exe
方法 2:您可以使用 安装 mitmproxypipx然后运行pipx inject mitmproxy <your dependency name>。有关详细信息,请参阅https://docs.mitmproxy.org/stable/overview-installation/#installation-from-the-python-package-index-pypi。
如果您从命令行进行调试(无论是 print 语句还是 pdb),最简单的方法是运行mitmdump而不是mitmproxy,它提供了相同的功能,但没有控制台界面。或者,您可以使用 PyCharm 的远程调试功能,该功能在控制台界面处于活动状态时也可以工作 ( https://github.com/mitmproxy/mitmproxy/blob/main/examples/contrib/remote-debug.py )。
| 归档时间: |
|
| 查看次数: |
4264 次 |
| 最近记录: |