python mitmproxy 简单使用

安装python库

pip install mitmproxy6.0.2 -i https:…pypi.douban.com/simple
pip uninstall MarkupSafe
pip install MarkupSafe
2.1.1 -i https:…pypi.douban.com/simple

安装证书

启动mitmproxy 代理

from mitmproxy import options
from mitmproxy import proxy
from mitmproxy.tools.dump import DumpMaster

class Counter:
    def __init__(self):
        pass


def run(port):
    myaddon = Counter()
    opts = options.Options(listen_port=port)
    pconf = proxy.config.ProxyConfig(opts)
    m = DumpMaster(opts, with_termlog=False, with_dumper=False)
    m.server = proxy.server.ProxyServer(pconf)
    m.addons.add(myaddon)

    print(f'启动监听 {port} 端口')
    try:
        m.run()
    except KeyboardInterrupt:
        m.shutdown()


if __name__ == "__main__":
    run(5670)

设置浏览器代理,访问 mitm.it
python mitmproxy 简单使用_第1张图片
安装证书

简单操作

class Counter:
    def __init__(self):
        pass

    def request(self, flow: mitmproxy.http.HTTPFlow):
        if "test_url" in flow.request.url:
            # 根据请求链接做处理
            pass
    def response(self, flow: mitmproxy.http.HTTPFlow):
        if "test_url" in flow.request.url:
            # 根据请求链接确定需要的请求 对响应做修改
            resp = flow.response.text
            result = json.loads(resp)
            # ... 修改操作
            flow.response.set_text(json.dumps(result))
            print("已修改响应内容")

简单的拦截修改响应内容就已经完成了
mitmproxy + selenium web注入js代码

你可能感兴趣的:(python,开发语言)