簡體   English   中英

在python中反序列化Protobuf 3 bytearray

[英]Deserialize Protobuf 3 bytearray in python

如何通過bytearray響應讀取Protobuf消息作為字符串?

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

當我嘗試mergeFrom時,mergeFromString返回獲取響應。 我收到了以下錯誤。

TypeError:MergeFrom()的參數必須是同一個類的實例:expected GetUpdateResponseMsg得到字節。

我嘗試了ParseFromString api並得到了無響應。

我試圖將Protobuf反序列化為人類可讀的格式。

還有什么我可以嘗試的嗎?

您需要反序列化響應。 傳入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是您將作為響應獲得的消息。

如果您有任何疑問,請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM