import asyncio
from mitmproxy import http
from mitmproxy.tools.dump import DumpMaster
from mitmproxy.options import Options
import json
class Intercept:
def request(self, flow: http.HTTPFlow) -> None:
# 拦截请求并检查 URL 是否包含特定路径
print("444444444")
if "http://example.com/path" in flow.request.pretty_url:
print("1111111111111111")
# 修改请求,例如添加自定义头
flow.request.headers["Custom-Header"] = "CustomValue"
def response(self, flow: http.HTTPFlow) -> None:
# 拦截响应并检查 URL 是否包含特定路径
print("333333333")
print(flow.response.text)
# if 'https://www.x.com/' in flow.request.pretty_url:
# print("2222222222")
# flow.response.text = "Hello World!"
async def start():
# 配置 mitmproxy 选项
options = Options(listen_host='127.0.0.1', listen_port=8080)
master = DumpMaster(options)
# 注册脚本
master.addons.add(Intercept())
try:
# 启动 mitmproxy
await master.run() # 使用 await 关键字等待 run 方法的执行
except KeyboardInterrupt:
# 停止代理时处理
await master.shutdown()
if __name__ == "__main__":
asyncio.run(start()) # 使用 asyncio.run() 启动异步函数
04-14
873
03-16
04-13
1264
02-22
1164