from mitmproxy import http
#1.方法request不能都修改
def request(flow: http.HTTPFlow):
#发起请求,且url是不是预期的url,如果是,创建url然后修改响应
if "quote.json" in flow.request.pretty_url:
with open("./code.json") as f:
#HTTPResponse.make响应修改
#实现打开本地文件,然后修改响应信息返回给前台
flow.response = http.HTTPResponse.make(
200,
f.read(),
{"Content-Type": "application/json"} # (optional) headers
)
json文件位置
为什么把json文件放在Python目录下,然后去运行mitmdupm命令。
然后再终端输入curl https://www.baidu.com/queto.json -x 127.0.0.1:8888,返回的数据就不是预期的json数据
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /queto.json was not found on this server.</p>
</body></html>
caohedeMBP:~ caohe$ curl http://www.baidu.com/