'Writes data to a socket and does not return until all the data is sent.'

def _sendall(sock, data):

    length = len(data)
    while length:
        try:
            sent = sock.send(data)
        except socket.error as e:
            if (e[0] == errno.EAGAIN):
                select.select([], [sock], [])
                continue
            else:
                raise
        data = data[sent:]
        length -= sent
