mitmproxy_test.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # -*- coding: utf-8 -*-
  2. # @Author: wangkun
  3. # @Time: 2023/7/24
  4. import os
  5. import time
  6. from mitmproxy import http, proxy, options, proxyconfig
  7. from mitmproxy.proxy.config import ProxyConfig
  8. from mitmproxy.tools.dump import DumpMaster
  9. from selenium import webdriver
  10. from selenium.webdriver import DesiredCapabilities
  11. from selenium.webdriver.chrome.service import Service
  12. class ProxyData:
  13. requests_data = []
  14. response_data = []
  15. @classmethod
  16. def start_proxy(cls):
  17. # 创建代理配置选项
  18. opts = options.Options(listen_host='0.0.0.0', listen_port=8888)
  19. # 创建代理配置
  20. config = ProxyConfig(opts)
  21. # 创建DumpMaster实例
  22. master = DumpMaster(opts)
  23. master.server = config
  24. # 启动代理
  25. print("Proxy started")
  26. master.run()
  27. @classmethod
  28. def intercept_request(cls, flow: http.HTTPFlow):
  29. # 拦截请求
  30. request_data = {
  31. 'url': flow.request.url,
  32. 'method': flow.request.method,
  33. 'headers': dict(flow.request.headers),
  34. 'content': flow.request.content.decode('utf-8')
  35. }
  36. cls.requests_data.append(request_data)
  37. @classmethod
  38. def intercept_response(cls, flow: http.HTTPFlow):
  39. # 拦截响应
  40. response_data = {
  41. 'url': flow.request.url,
  42. 'status_code': flow.response.status_code,
  43. 'headers': dict(flow.response.headers),
  44. 'content': flow.response.content.decode('utf-8')
  45. }
  46. cls.response_data.append(response_data)
  47. @classmethod
  48. def start_selenium(cls):
  49. quit_cmd = "ps aux | grep Chrome | grep -v grep | awk '{print $2}' | xargs kill -9"
  50. os.system(quit_cmd)
  51. time.sleep(1)
  52. # 启动 Chrome,指定端口号:8888
  53. cmd = 'open -a "Google Chrome" --args --remote-debugging-port=8888'
  54. os.system(cmd)
  55. # 打印请求配置
  56. ca = DesiredCapabilities.CHROME
  57. ca["goog:loggingPrefs"] = {"performance": "ALL"}
  58. # 配置 chromedriver
  59. chromedriver = "/Users/wangkun/Downloads/chromedriver/chromedriver_v114/chromedriver"
  60. # 初始化浏览器
  61. browser = webdriver.ChromeOptions()
  62. browser.add_experimental_option("debuggerAddress", "127.0.0.1:8888")
  63. # driver初始化
  64. driver = webdriver.Chrome(desired_capabilities=ca, options=browser, service=Service(chromedriver))
  65. driver.implicitly_wait(10)
  66. print("打开抖音推荐页")
  67. driver.get(f"https://www.douyin.com/")
  68. if __name__ == "__main__":
  69. ProxyData.start_proxy()
  70. ProxyData.start_selenium()
  71. print("requests_data:", ProxyData.requests_data)
  72. print("response_data:", ProxyData.response_data)
  73. # 分析包含链接 www.douyin.com 的响应数据
  74. for response in ProxyData.response_data:
  75. if "www.douyin.com" in response['url']:
  76. print("Douyin response:", response)