首页 > 解决方案 > 如何使用 mitmproxy 从什么流中获取一些块?

问题描述

我正在使用以下内容构建 Web 代理mitmproxyhttps://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 功能中获得一些块。

标签: mitmproxy

解决方案


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

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)

推荐阅读