python example for reading multiple protobuf messages from a stream

前端 未结 3 2059
情歌与酒
情歌与酒 2021-02-05 09:14

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

3条回答
  •  天涯浪人
    2021-02-05 09:49

    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 varint32s 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 varint32s 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
    

提交回复
热议问题