def recvall(sock, n):
    """
    Reads bytes from the given socket stream as long as the received data length is lower than n.
    :param sock: Socket object to receive data.
    :param n: The target length of the message.
    """

    data = ''
    while len(data) < n:
        packet = sock.recv(n - len(data))
        if not packet:
            return None
        data += packet
    return data