3

我正在使用 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 编码的响应体?

4

2 回答 2

6

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

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

def response(flow):
    print(assemble_request(flow.request).decode('utf-8'))
于 2019-04-29T00:01:37.630 回答
0

您可以使用flow.request.assemble(). 如果您想要没有传输编码(gzip)的请求/响应,您可以使用解码的装饰器:

from libmproxy.protocol.http import decoded

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

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

于 2015-02-20T13:34:10.777 回答