项目版本

本项目用到的Python版本为3.7.9,如果出现不兼容情况,请尝试切换Python版本

项目依赖 安装指令 项目使用版本
mitmproxy pip install mitmproxy==5.3.0 5.3.0

简述对象用途

对象名字 类型 作用
Request HTTP请求类 发起request
Response HTTP响应类 接收response
Message HTTP信息类 处理HTTP信息
MultiDictView 字典工具类 可迭代方便后续处理

官方HTTP模板

以下是官方的HTTP事件模板
不过常用的只有request和response事件
所以我们要对模板删减一部分

  1. """HTTP-specific events."""
  2. import mitmproxy.http
  3. class Events:
  4. def http_connect(self, flow: mitmproxy.http.HTTPFlow):
  5. """
  6. An HTTP CONNECT request was received. Setting a non 2xx response on
  7. the flow will return the response to the client abort the
  8. connection. CONNECT requests and responses do not generate the usual
  9. HTTP handler events. CONNECT requests are only valid in regular and
  10. upstream proxy modes.
  11. """
  12. def requestheaders(self, flow: mitmproxy.http.HTTPFlow):
  13. """
  14. HTTP request headers were successfully read. At this point, the body
  15. is empty.
  16. """
  17. def request(self, flow: mitmproxy.http.HTTPFlow):
  18. """
  19. The full HTTP request has been read.
  20. """
  21. def responseheaders(self, flow: mitmproxy.http.HTTPFlow):
  22. """
  23. HTTP response headers were successfully read. At this point, the body
  24. is empty.
  25. """
  26. def response(self, flow: mitmproxy.http.HTTPFlow):
  27. """
  28. The full HTTP response has been read.
  29. """
  30. def error(self, flow: mitmproxy.http.HTTPFlow):
  31. """
  32. An HTTP error has occurred, e.g. invalid server responses, or
  33. interrupted connections. This is distinct from a valid server HTTP
  34. error response, which is simply a response with an HTTP error code.
  35. """

启动Mitmproxy

我们根据官方插件开发的教程

名称 命名要求 类型 用途
Counter 任意命名 class 插件实例
addons 必须为addons list 记录多个插件(任务列表)

新建addons.py文件并写入以下内容

  1. import mitmproxy.http
  2. from mitmproxy import ctx
  3. class Counter:
  4. def request(self, flow: mitmproxy.http.HTTPFlow):
  5. ctx.log.info('白色标准输出:{}'.format(flow.request.url))
  6. ctx.log.warn('黄色警告输出:{}'.format(flow.request.url))
  7. ctx.log.error('红色异常输出:{}'.format(flow.request.url))
  8. def response(self, flow: mitmproxy.http.HTTPFlow):
  9. print(flow.response.status_code)#获取网页状态码
  10. addons = [
  11. Counter()
  12. ]

随后在终端输入mitmweb -s addons.py启动插件
显示以下内容说明成功启动mitmproxy
自动弹出mitmweb默认自带管理界面http://127.0.0.1:8081

  1. Web server listening at http://127.0.0.1:8081
  2. Loading script addons.py
  3. Proxy server listening at http://*:8080

连接Mitmproxy

安装requests模块,用来发起HTTP请求并连接8080端口

  1. pip install requests

新建http_demo.py文件并写入以下内容 ```python import requests

headers = { ‘User-Agent’:’Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36’ }

proxies = { ‘http’:’127.0.0.1:8080’, ‘https’:’127.0.0.1:8080’, }

url = ‘https://www.baidu.com

html = requests.get(url,headers=headers,proxies = proxies,verify=False) print(html.status_code) ```

浏览器访问http://127.0.0.1:8081进入mitmweb管理界面
在终端输入python http_demo.py运行Python文件
此时mitmweb管理界面就会出现抓包内容