简体   繁体   English

在python中反序列化Protobuf 3 bytearray

[英]Deserialize Protobuf 3 bytearray in python

How to read Protobuf message through bytearray response as string? 如何通过bytearray响应读取Protobuf消息作为字符串?

I tried looking up Protobuf library. 我试着查找Protobuf库。 https://developers.google.com/protocol-buffers/docs/reference/python/google.protobuf.message-pysrc#Message.MergeFrom https://developers.google.com/protocol-buffers/docs/reference/python/google.protobuf.message-pysrc#Message.MergeFrom

When I tried mergeFrom , mergeFromString to fetch response back. 当我尝试mergeFrom时,mergeFromString返回获取响应。 I am getting below error. 我收到了以下错误。

TypeError: Parameter to MergeFrom() must be instance of same class: expected GetUpdateResponseMsg got bytes. TypeError:MergeFrom()的参数必须是同一个类的实例:expected GetUpdateResponseMsg得到字节。

I tried ParseFromString api and got None response back. 我尝试了ParseFromString api并得到了无响应。

I am trying to deserialize Protobuf back to human readable format. 我试图将Protobuf反序列化为人类可读的格式。

Is there anything else I can try? 还有什么我可以尝试的吗?

You need to deserialize the response. 您需要反序列化响应。 Pass in the class/protobuf type along with message and you should get the response in the format.. Sample example would be: 传入class / 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 is message which you will get as response. byte_message是您将作为响应获得的消息。

Let me know if you have any questions. 如果您有任何疑问,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM