from mitmproxy import http from mitmproxy.tools.main import mitmdump class AddHeader: def __init__(self): self.num = 0 self.file = open("./request_headers.txt", "w") # 打开文件对象 def request(self, flow: http.HTTPFlow) -> None: self.num += 1 flow.request.headers["My-Header"] = f"Request {self.num}" print("Request Headers:") for name, value in flow.request.headers.items(): print(f"{name}: {value}") # 保存请求头部内容到文件 self.file.write(f"Request {self.num} Headers:\n") for name, value in flow.request.headers.items(): self.file.write(f"{name}: {value}\n") self.file.write("\n") self.file.flush() # 立即写入文件 # def response(self, flow: http.HTTPFlow) -> None: # self.num += 1 # flow.response.headers["My-Header"] = f"Response {self.num}" # print("Response Headers:") # for name, value in flow.response.headers.items(): # print(f"{name}: {value}") def done(self): self.file.close() # 关闭文件对象 addons = [ AddHeader() ] if __name__ == "__main__": mitmdump(['-p', '8081', '-s', __file__]) 以上代码中 如何将请求的URL一同保存到request_headers.txt中
时间: 2024-03-11 20:50:42 浏览: 144
解决H5py的DLL问题:from . import h5 as _h5
3星 · 编辑精心推荐
可以在保存请求头部内容的时候,将请求的URL也一同写入文件中,修改代码如下:
```
# 保存请求头部内容和请求URL到文件
self.file.write(f"Request {self.num} Headers:\n")
for name, value in flow.request.headers.items():
self.file.write(f"{name}: {value}\n")
self.file.write(f"URL: {flow.request.pretty_url}\n\n")
self.file.flush()
```
这样,请求头部内容和请求URL就都会被写入到request_headers.txt文件中了。
阅读全文