https://github.com/ring04h/wyproxy
1.上面链接是python2.7姐和mimtproxy做代理的,下面将使用python3.6开启mimtproxy服务
(1)开启mimtproxy服务
from mitmproxy.master import Master from mitmproxy.proxy import config from mitmproxy import options from mitmproxy.proxy.server import ProxyServer import abon opts = options.Options() opts.add_option( "listen_host", str, "127.0.0.1", "Address to bind proxy to." ) opts.add_option( "listen_port", int, 8080, "Proxy service port." ) opts.add_option( "mode", str, "regular", """ Mode can be "regular", "transparent", "socks5", "reverse:SPEC", or "upstream:SPEC". For reverse and upstream proxy modes, SPEC is host specification in the form of "http[s]://host[:port]". """ ) #这个参数用于http1.py加载数据时候的最大大小 opts.add_option( "body_size_limit", int, 1000000000, """ Mode can be "regular", "transparent", "socks5", "reverse:SPEC", or "upstream:SPEC". For reverse and upstream proxy modes, SPEC is host specification in the form of "http[s]://host[:port]". """ ) cf = config.ProxyConfig(options=opts) server = ProxyServer(config=cf) ms = Master(opts=opts) ms.server = server #最关键的一步用于添加addon ms.addons.add(abon.filterRq()) print('代理服务器启动了') ms.run()
(2)创建addons,用于处理request和response
import typing from mitmproxy import http class filterRq: def __init__(self): self.lst = [] def load(self, loader): loader.add_option( "filterRq", typing.Sequence[str], [], """ Header set pattern of the form "/pattern/header/value", where the separator can be any character. """ ) def request(self, flow:http.HTTPFlow): print(flow.request.url) def response(self, flow): print(flow.response.url)
(3)selenium测试
from selenium.webdriver.chrome import webdriver import time from selenium.webdriver.chrome import options optss = options.Options() optss.add_argument("--proxy-server=http://127.0.0.1:8080") wb = webdriver.WebDriver(executable_path='D:\\chromedriver_win32\\chromedriver.exe',options=optss) wb.get("https://www.baidu.com/") time.sleep(10)
结果:
https://www.baidu.com/
https://www.baidu.com/img/bd_logo1.png
https://www.baidu.com/img/bd_logo1.png?qua=high
https://www.baidu.com/img/baidu_jgylogo3.gif
https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/static/protocol/https/jquery/jquery-1.10.2.min_65682a2.js
https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/static/protocol/https/global/img/icons_5859e57.png
https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/static/protocol/https/home/img/qrcode/zbios_efde696.png
https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/static/protocol/https/global/js/all_async_search_55e56a3.js
https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/static/protocol/https/plugins/every_cookie_4644b13.js
https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/static/protocol/https/home/js/nu_instant_search_b73e920.js
https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/static/protocol/https/global/img/quickdelete_33e3eb8.png
https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/static/protocol/https/plugins/swfobject_0178953.js
https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/static/protocol/https/soutu/js/tu_d03f361.js
https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/static/protocol/https/sug/js/bdsug_async_125a126.js
https://www.baidu.com/his?wd=&from=pc_web&rf=3&hisdata=&json=1&p=3&sid=1430_21126_26350&req=2&csor=0&cb=jQuery110208383136235858786_1543739464966&_=1543739464967
https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/static/protocol/https/soutu/css/soutu.css
https://www.baidu.com/img/baidu_85beaf5496f291521eb75ba38eacbd87.svg
https://www.baidu.com/favicon.ico
https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/static/protocol/https/soutu/img/camera_new_x2_fb6c085.png
总结:
1.可以将上述代码封装
2.cer ssl文件在~/.mimtproxy文件夹下(windows在用户目录下,linux在/home/用户底下,启动的时候自动生成)