Comment #10 on issue 226 by [email protected]: Python API doesn't
support reading/writing delimited messages
https://code.google.com/p/protobuf/issues/detail?id=226
Here's a workaround - assuming you want to do this because you are writing
protobuf messages to sockets. The delimited write simply writes the message
length as a varint32 before writing the message itself:
(apologies for unidiomatic code - bit of a python noob)
from google.protobuf.internal.decoder import _DecodeVarint32
from google.protobuf.internal.encoder import _EncodeVarint
def write_message_delimited(socket, msg):
hdr = []
_EncodeVarint(hdr.append, len(msg))
socket.sendall("".join(hdr))
socket.sendall(msg.SerializeToString())
def read_message_delimited(socket):
# int length is at most 4 bytes long
hdr_bytes = socket.recv(4)
(msg_length, hdr_length) = _DecodeVarint32(hdr_bytes, 0)
rsp_buffer = io.BytesIO()
if hdr_length < 4:
rsp_buffer.write(hdr_bytes[hdr_length:])
# read the remaining message bytes
msg_length = msg_length - (4 - hdr_length)
while msg_length > 0:
rsp_bytes = socket.recv(min(8096, msg_length))
rsp_buffer.write(rsp_bytes)
msg_length = msg_length - len(rsp_bytes)
return MyMessage.ParseFromString(rsp_buffer.getvalue())
--
You received this message because this project is configured to send all
issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings
--
You received this message because you are subscribed to the Google Groups "Protocol
Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.