Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Protocol Buffers (protobuf) in Python3 - trouble with ParseFromString (encoding?)

I've got Google Protocol buffers 80% working in Python3. My .proto file works, I'm encoding data, life is almost good. The problem is that I can't ParseFromString the result of SerializeToString. When I print SerializeToString it looks like what I'd expect, a fairly compact binary representation (preceded by b').

My guess is that perhaps this is a difference in the way Python2 and Python3 handle strings. The putput of SerializeToString is Bytes, not a string.

Printed output of SerializeToString (Python type is ):

b'\x10\xd7\xeb\x8e\xcd\x04\x1a\x0cnamegoeshere2@\x08\x80\xf8\xde\xc3\x9f\xb0\x81\x89\x14\x11\x00\x00\x00\x00\x00\x80d\xc0\x19\x00\x00\x00\x00\x00\xc0m@!\x00\x00\x00\x00\x00\x80R\xc0)\x00\x00\x00\x00\x00x\xb7\xc01\x00\x00\x00\x00\x00\x8c\x95@9\x00\x00\x00\x00\x00\x16\xb2@'

result of ParseFromString(message):

None

No error is provided...

So - my best guess is that all I need to do is .decode() the bytes object generated, the problem is that I have no clue what the encoding is. I've tried UTF-8, -16, Latin-1, and a few others without success. My Google-Fu is strong but I haven't found anything on this.

Any help would be appreciated.

like image 530
EnemyBagJones Avatar asked Nov 20 '15 00:11

EnemyBagJones


People also ask

What encoding does Protobuf use?

Protobuf strings are always valid UTF-8 strings. See the Language Guide: A string must always contain UTF-8 encoded or 7-bit ASCII text.

What is ParseFromString in Python?

ParseFromString is a method -- it does not return anything, but rather fills in self with the parsed content. Use it like: message = MyMessageType() message.ParseFromString(data) print message.some_field.

How does Protobuf encode data?

A protocol buffer message is a series of key-value pairs. The binary version of a message just uses the field's number as the key -- the name and declared type for each field can only be determined on the decoding end by referencing the message type's definition (that is, the . proto file).


1 Answers

ParseFromString is a method -- it does not return anything, but rather fills in self with the parsed content. Use it like:

message = MyMessageType()
message.ParseFromString(data)
print message.some_field
like image 155
Kenton Varda Avatar answered Oct 24 '22 10:10

Kenton Varda