我如何从使用mitmproxy的流中获取一些块?

时间:2019-11-05 00:34:46

标签: mitmproxy

我正在使用以下mitmproxy构建Web代理。 https://github.com/mitmproxy/mitmproxy

我想扫描流媒体正文并将其传递给客户端。 mitmproxy已具有类似功能; https://github.com/mitmproxy/mitmproxy/blob/master/mitmproxy/addons/streambodies.py

我实现了以下代码。

def responseheaders(self, flow):
    flow.response.stream = self.response_stream

def response_stream(self, chunks):
    for chunk in chunks:
        if not my_function(chunk):
            raise Exception('catch')
         yield chunk

在我上面的代码中,没有办法捕获my_function返回False时来自其流程的某些块。

我想从flow.response.stream功能中获取一些块。

1 个答案:

答案 0 :(得分:0)

我用以下代码解决了我的问题。

from functools import partial

def responseheaders(self, flow):
    flow.response.stream = partial(self.response_stream, flow=flow)

def response_stream(self, chunks, flow):
    for chunk in chunks:
        yield chunk

否则,在对象中使用__call__可能是解决方案之一。

class Streamer:
    def __init__(self, flow):
        self.flow = flow

    def __call__(self, chunks):
        for chunk in chunks:
            yield chunk


def responseheaders(self, flow):
    flow.response.stream = Streamer(flow)
相关问题