I am trying to receive a protobuf message, which was send from a java application with "writeDelmitedTo()" inside my python application.
After some research I already came across this code to read the message from the socket, decode it and parse it.
data = sock.recv()
(size, position) = decoder._DecodeVarint(data, 0)
msg = MessageWrapper_pb2.WrapperMessage().ParseFromString(data[position:position + size])
What I am getting now is a google.protobuf.message.DecodeError: Truncated message Exception.
Has anyone encountered a similar problem or knows how to read delimited data from a socket and parse it correctly?
Edit:
This is the solution that worked for me.
def read_java_varint_delimited_stream(sock):
buf = []
data = sock.recv()
rCount = len(data)
(size, position) = decoder._DecodeVarint(data, 0)
buf.append(data)
while rCount < size+position:
data = sock.recv(size+position-rCount)
rCount += len(data)
buf.append(data)
return b''.join(buf), size, position
def readMessage(sock):
data, size, position = read_java_varint_delimited_stream(sock)
msg = MessageWrapper_pb2.WrapperMessage()
msg.ParseFromString(data[position:position + size])
return msg