在python中反序列化Protobuf 3字节数组



如何通过字节数组响应以字符串形式读取Protobuf消息?

我尝试查找Protobuf库。 https://developers.google.com/protocol-buffers/docs/reference/python/google.protobuf.message-pysrc#Message.MergeFrom

当我尝试 mergeFrom 时,mergeFromString 来获取响应。 我得到以下错误。

TypeError: MergeFrom(( 的参数必须是同一类的实例:预期的 GetUpdateResponseMsg 获取字节。

我尝试了 ParseFromString api,并得到了 None 响应。

我正在尝试将 Protobuf 反序列化回人类可读的格式。

还有什么我可以尝试的吗?

您需要反序列化响应。将类/protobuf 类型与消息一起传递,您应该以以下格式获得响应。 示例示例为:

from BusinessLayer.py.GetDealUpdateData_pb2 import GetDealUpdateResponseDM
from importlib import import_module
def deserialize(byte_message, proto_type):
module_, class_ = proto_type.rsplit('.', 1)
class_ = getattr(import_module(module_), class_)
rv = class_()
rv.ParseFromString(byte_message)
return rv
print (deserialize(byte_message, 'BusinessLayer.py.GetDealUpdateData_pb2.GetDealUpdateResponseDM'))

byte_message是您将收到的响应的消息。

如果您有任何问题,请告诉我。

最新更新