I\'m working with data from spinn3r, which consists of multiple different protobuf messages serialized into a byte stream:
http://code.google.com/p/spinn3r-client/wiki/P
It looks like the code in the other answer is potentially lifted from here. Check the licence before using this file but I managed to get it to read varint32
s using code such as this:
import sys
import myprotocol_pb2 as proto
import varint # (this is the varint.py file)
data = open("filename.bin", "rb").read() # read file as string
decoder = varint.decodeVarint32 # get a varint32 decoder
# others are available in varint.py
next_pos, pos = 0, 0
while pos < len(data):
msg = proto.Msg() # your message type
next_pos, pos = decoder(data, pos)
msg.ParseFromString(data[pos:pos + next_pos])
# use parsed message
pos += next_pos
print "done!"
This is very simple code designed to load messages of a single type delimited by varint32
s which describe the next message's size.
Update: It may also be possible to include this file directly from the protobuf library by using:
from google.protobuf.internal.decoder import _DecodeVarint32