Discussion forum for David Beazley

Correct way to get exactly n bytes from curio socket


If I have a wire protocol with fixed length messages, is it a good idea to do something like the following to get the whole message?

async def get_n_bytes(self, nbytes):
    data = b''
    while len(data) < nbytes:
        read = await self.client_sock.recv(nbytes-len(data))
        if not read:
            raise ConnectionClosedUnexpectedlyByPeer
        data += read
    return data