'Attempts to receive length bytes from a socket, blocking if necessary.
(Socket may be blocking or non-blocking.)'

def _recvall(sock, length):

    dataList = []
    recvLen = 0
    while length:
        try:
            data = sock.recv(length)
        except socket.error as e:
            if (e[0] == errno.EAGAIN):
                select.select([sock], [], [])
                continue
            else:
                raise
        if (not data):
            break
        dataList.append(data)
        dataLen = len(data)
        recvLen += dataLen
        length -= dataLen
    return (''.join(dataList), recvLen)
