gpt4 book ai didi

python - 在python中发送application/x-protobuf文件

转载 作者:太空宇宙 更新时间:2023-11-03 17:32:42 26 4
gpt4 key购买 nike

我正在编写一个 mitmproxy 脚本,该脚本拦截 application/x-protobuf 文件并进行一些修改,然后将其转发给客户端。这是代码:

from cStringIO import StringIO
from libmproxy.protocol.http import decoded
import re
def response(context, flow):

if flow.response.headers.get_first("content-type", "").startswith('application/x-protobuf'):
with decoded(flow.response):
data=flow.response.content
data2=re.sub('"http://.*?"','"http://some other url"' ,data)
flow.response.content=data2

但是,当客户端收到文件时,它会抛出“无法解析输入”。错误

最佳答案

这看起来像问题:

re.sub('"http://.*?"','"http://some other url"' ,data)

您不能使用正则表达式重写 protobuf 中嵌入的 URL。 Protobuf 是一种使用基于长度的分隔符的二进制编码。因此,例如,当一个字符串嵌入到 protobuf 中时,它会以长度为前缀。如果您使用正则表达式来更改字符串,那么长度将是错误的。请注意,您不能只更改长度,因为父对象的长度可能仍然是错误的,等等。

您需要使用 Protobuf Python 库解码 protobuf 数据,编辑需要更改的字段,然后重新编码。

关于python - 在python中发送application/x-protobuf文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31663463/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com