Deserialize Protobuf 3 bytearray in python

How can I read a Protobuf message through a bytearray response as a string?

I tried to find the Protobuf library. https://developers.google.com/protocol-buffers/docs/reference/python/google.protobuf.message-pysrc#Message.MergeFrom

When I tried mergeFrom, mergeFromString to return a response. I am getting below error.

TypeError: The MergeFrom () parameter must be an instance of the same class: GetUpdateResponseMsg is expected to receive bytes.

I tried the ParseFromString api and got a No Response response.

I am trying to deserialize Protobuf back to human readable format.

Is there anything else I can try?

+3


source to share


1 answer


You need to deserialize the response. Go to the / protobuf type class along with the message and you should get a response in the format .. Example example:

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 the message you will receive as a response.

Let me know if you have any questions.

+2


source







All Articles