while True:
    data += self.request.recv()
if not data:
    break
buffer += data
while True:
    if length is None:
        if ':' not in buffer:
            break
length_str, ignored, buffer = buffer.partition(':')
length = int(length_str)
if len(buffer) < length:
    break
message = buffer[:length]
buffer = buffer[length:]
length = None
Python socket receive - incoming packets always have a different size