mitmproxy 打包成exe 进行抓包
直接上代码
# -- coding: utf-8 --
from mitmproxy.options import Options
from mitmproxy.proxy.config import ProxyConfig
from mitmproxy.proxy.server import ProxyServer
from mitmproxy.tools.dump import DumpMaster
import threading
import asyncio
import time
class Addon(object):
def __init__(self):
self.num = 1
def request(self, flow):
url = flow.request.url
print('requrl',url)
flow.request.headers["count"] = str(self.num)
print('req',flow.request.content.decode())
# print(flow.request.text)
if 'baidu' in url:
flow.kill()
def response(self, flow):
url = flow.request.url
# if 'baidu' in url:
self.num = self.num + 1
flow.response.headers["count"] = str(self.num)
# print(self.num)
print('resp',flow.response.content.decode())
# print(flow.response.content)
# see source mitmproxy/master.py for details
def loop_in_thread(loop, m):
asyncio.set_event_loop(loop) # This is the key.
m.run_loop(loop.run_forever)
if __name__ == "__main__":
options = Options(listen_host='0.0.0.0', listen_port=888, )
m = DumpMaster(options, with_termlog=False, with_dumper=False)
config = ProxyConfig(options)
m.server = ProxyServer(config)
m.addons.add(Addon())
# run mitmproxy in backgroud, especially integrated with other server
loop = asyncio.get_event_loop()
t = threading.Thread(target=loop_in_thread, args=(loop,m) )
t.start()
# t.setDaemon(True)
# Other servers might be started too.
# time.sleep(20)
# # print('going to shutdown mitmproxy')
# m.shutdown()