好友
阅读权限20
听众
最后登录1970-1-1
|
楼主|
Glenn
发表于 2022-9-6 11:17
本帖最后由 Glenn 于 2022-9-6 11:20 编辑
已用5.2版本的mitmproxy 解决。附上代码:
# -*-coding:utf-8-*-
import time
import queue
from multiprocessing import Process, freeze_support, Queue
from mitmproxy import proxy, options
from mitmproxy.http import HTTPFlow
from mitmproxy.tools.dump import DumpMaster
import sys
class Handler:
"""
将请求加入队列
"""
def __init__(self, req_queue: Queue):
self.req_queue = req_queue
def request(self, flow: HTTPFlow):
request_dict = {
"method": flow.request.method,
"url": flow.request.url,
"headers": [(key, flow.request.headers[key]) for key in flow.request.headers],
"cookies": [(key, flow.request.cookies[key]) for key in flow.request.cookies],
"data": flow.request.text
}
try:
print(request_dict.get("url"))
print(request_dict.get("headers"))
self.req_queue.put(request_dict, timeout=2)
except queue.Full as e:
print(e)
def response(self, flow: HTTPFlow):
pass
def responseheaders(self, flow: HTTPFlow):
pass
def do_start_proxy(req_queue):
opts = options.Options(listen_host="127.0.0.1", listen_port=8888, ssl_insecure=True)
m = DumpMaster(opts, with_termlog=False, with_dumper=False)
pconf = proxy.config.ProxyConfig(opts)
m.server = proxy.server.ProxyServer(pconf)
m.addons.add(Handler(req_queue))
print("Proxy server listening at http://127.0.0.1:8888")
m.run() # 启动后就在这里循环
m.shutdown()
def start_proxy(req_queue):
freeze_support()
p = Process(target=do_start_proxy, args=(req_queue, ))
p.start()
return p
if __name__ == "__main__":
req_queue = Queue(6000)
p = start_proxy(req_queue)
try:
while True:
time.sleep(10)
break
except KeyboardInterrupt:
p.terminate() |
|