MITM代理,获取整个请求和响应字符串

问题描述 投票:1回答:2

我正在使用mitmproxy拦截流量。我想要的是能够在字符串中获取整个请求和响应。我知道你有def response(context, flow),HTTPFlow对象有请求和响应对象。我想要的只是字符串中的这样的东西

GET http://www.google-analytics.com/collect?v=1& HTTP/1.1
Header 1: value
Header 2: value

request body

还有这个

HTTP/1.1 301 Moved Permanently
Header 1: value
Header 2: value

response body

现在我一直在尝试加入请求和响应的不同部分和位,但这很容易出错。有一个更好的方法吗?

另外,mitm是否处理Gzip编码的响应体?

python proxy mitmproxy
2个回答
0
投票

您可以使用flow.request.assemble()将整个请求/响应对象作为字符串。如果你想要没有transfer-encoding(gzip)的请求/响应,你可以使用解码的装饰器:

from libmproxy.protocol.http import decoded

with decoded(flow.request):
    data = flow.request.assemble()

除此之外,你可能会发现https://github.com/mitmproxy/mitmproxy/tree/master/examples非常有用。


0
投票

如果有人碰到这个;上面的答案不适用于mitmproxy 4.相反,人们可以使用这个:

from mitmproxy.net.http.http1.assemble import assemble_request

def response(flow):
    print(assemble_request(flow.request).decode('utf-8'))
© www.soinside.com 2019 - 2024. All rights reserved.