mitmproxy - 如何使用 mitmproxy 从什么流中获取一些块?
问题描述
我正在使用以下内容构建 Web 代理mitmproxy
。
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 功能中获得一些块。
解决方案
我用下面的代码解决了我的问题。
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)
推荐阅读
- node.js - 使用 webpack 将 typescript 编译为 js 并保持目录结构
- javascript - 如何在 Chrome 中的 HTML 元素上获取动画()?
- validation - Yii2:“保存草稿”按钮
- php - 如何在 netlify 上运行 php 文件?
- android - 应用程序之间的通信,startService() 或 sendBroadcast()
- c - 从文件中计算矩阵中的岛屿
- php - 使用 https 重定向配置 .htaccess mod_rewrite
- c# - 部署错误值不在windows forms应用程序的预期范围内
- python - 无法将 kivy 远程服务器连接到 ubuntu 18.04
- branch - 在远程仓库中创建了新分支,现在无法在本地签出新分支