pip install -i https://mirrors.aliyun.com/pypi/simple/ mitmproxy
https 抓取: 安装证书
去到用户目录下的 .mitmproxy 文件夹,将对应的证书复制出来
双击打开,然后去设置信任证书
然后编写脚本
import requests
import os
from mitmproxy import http, ctx
class Handler:
def request(self, flow: http.HTTPFlow):
method = flow.request.method
host = flow.request.host
url = flow.request.url
ctx.log(f'url: {url}')
# 拦截请求不发到服务器,直接返回结果,也可以用URL匹配
# if 'xxx.net' not in host and 'xxx.xxx' not in host:
# flow.response = http.Response.make(204)
# return
def response(self, flow):
url = flow.request.url
method = flow.request.method
content = flow.response.content
status_code = flow.response.status_code
resp_headers = flow.response.headers
current_folder = os.path.dirname(os.path.abspath(__file__))
download_folder = os.path.join(current_folder, 'test')
# 保存想要的文件
if 'xxx' not in url and '.' in url and method == 'GET':
file_name = url.split("/")[-1]
with open(os.path.join(download_folder, file_name), 'wb+') as f:
f.write(content)
f.close()
addons = [Handler()]
1. mitmproxy -s 脚本文件执行脚本,抓包日志就会打印在该文件中
如果想要debug上面的脚本,可以新建一个脚本 xxx.py
import os
import sys
from mitmproxy.tools.main import mitmdump
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
mitmdump(['-s', '上面脚本文件'])
然后debug方式启动xxx.py, 在具体的拦截脚本中打断点,就能够调试了。
2. 也可以直接mitmweb 打开网页查看抓包记录