Skip to content

Instantly share code, notes, and snippets.

@sdamashek
Created February 18, 2017 05:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdamashek/aaf7b315ff01382d84ed29d08da8e391 to your computer and use it in GitHub Desktop.
Save sdamashek/aaf7b315ff01382d84ed29d08da8e391 to your computer and use it in GitHub Desktop.
Protobuf lengths
from google.protobuf.internal.decoder import _DecodeVarint
from google.protobuf.internal.encoder import _VarintBytes
import datapoint_pb2
def read_message(buf): # buf is a bytestring
pos = 0 # Start of varint32
value, new_pos = _DecodeVarint(buf, pos)
msg = datapoint_pb2.DataPoint()
msg.ParseFromString(buf[new_pos:new_pos+value])
return msg
def write_message(msg, stream):
message = msg.SerializeToString()
delimiter = _VarintBytes(len(message))
stream.write(delimter + message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment