Comment #9 on issue 226 by Python API doesn't support reading/writing delimited messages

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

def read_message_delimited(socket):
    # int length is at most 4 bytes long
    hdr_bytes = self.socket.recv(4)
    (msg_length, hdr_length) = _DecodeVarint32(hdr_bytes, 0)

    rsp_buffer = io.BytesIO()
    if hdr_length < 4:

    # read the remaining message bytes
    msg_length = msg_length - (4 - hdr_length)
    while msg_length > 0:
        rsp_bytes = self.socket.recv(min(8096, msg_length))
        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:

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 post to this group, send email to
Visit this group at
For more options, visit

Reply via email to