Reputation: 4082
I have a project and I need to create an HTTP packet interceptor in python as a proxy, but I can not use mitmproxy because it only works from the cli command and not from an internal library. I have an application and I can not tell people to run mitmproxy because that's what the app should do internally. I'm currently using a fork to launch mitmproxy from bash, but it gives me a lot of trouble creating this functionality for other operating systems.
For this reason, someone knows some way to intercept traffic as a proxy that does not depend on a command outside python ?.
Upvotes: 0
Views: 2505
Reputation: 6682
mitmproxy can be load inside your python code.
from mitmproxy import proxy, options
from mitmproxy.tools.dump import DumpMaster
from mitmproxy.addons import core
class AddHeader:
def __init__(self):
self.num = 0
def response(self, flow):
self.num = self.num + 1
print(self.num)
flow.response.headers["count"] = str(self.num)
addons = [
AddHeader()
]
if __name__ == '__main__':
opts = options.Options(listen_host='127.0.0.1', listen_port=8080)
pconf = proxy.config.ProxyConfig(opts)
m = DumpMaster(None)
m.server = proxy.server.ProxyServer(pconf)
print(m.addons)
m.addons.add(addons)
# m.addons.add(core.Core())
try:
m.run()
except KeyboardInterrupt:
m.shutdown()
This code is taken from the GitHub issue.
Upvotes: 1