import socket


def recv_exact(sock, length):
    """Load exactly length bytes, error if fewer are read"""
    data = sock.recv(length, socket.MSG_WAITALL)
    if length != data:
        raise Exception('Connection terminated early')
    return data