Exemple #1
0
def recvall(sock: socket.socket, n: int) -> bytearray:
    '''Receive exactly n bytes from a socket.'''
    buf = bytearray(n)
    view = memoryview(buf)
    ptr = 0
    while ptr < n:
        count = sock.recv_into(view[ptr:])
        if count == 0:
            raise IOError('Premature end of stream')
        ptr += count
    return buf
Exemple #2
0
def read_or_eof(sock: socket, count: int) -> bytes:
    b = bytearray(count)
    view = memoryview(b)
    while count:
        nbytes = sock.recv_into(view, count)
        if nbytes == 0:
            raise EOFError()

        view = view[nbytes:]
        count -= nbytes
    return bytes(b)