MITM Proxy, getting entire request and response string

核能气质少年 提交于 2019-12-08 06:56:28

问题


I am using mitmproxy for intercepting traffic. What I want is to be able to get the entire request and response in a string. I know that you have def response(context, flow) and that the HTTPFlow object has the request and response objects. What I want is simply something like this in a string

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

request body

and this

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

response body

Now I've been trying this by joing the different parts and bits of the requests and responses but that is prone to errors. Is there a better way to do this?

Also, does mitm handle Gzip encoded response bodies?


回答1:


If some one bumps into this; the above answer does not work for mitmproxy 4. Instead one can use this:

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

def response(flow):
    print(assemble_request(flow.request).decode('utf-8'))



回答2:


You can get the whole request/response object as a string using flow.request.assemble(). If you want the request/response without transfer-encoding (gzip), you can use the decoded decorator:

from libmproxy.protocol.http import decoded

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

Apart from that, you may find https://github.com/mitmproxy/mitmproxy/tree/master/examples very useful.



来源:https://stackoverflow.com/questions/28626213/mitm-proxy-getting-entire-request-and-response-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!