pyngus/sockets.py
def write_socket_output(connection, socket_obj):
    """Write data to the network layer.  Can support both blocking and
    non-blocking sockets.
    Returns the number of output bytes sent, or EOS if output processing
    is done.  Any exceptions raised by the socket are re-raised.
    """
    count = connection.has_output
    if count <= 0:
        return count  # 0 or EOS

    data = connection.output_data()
    if not data:
        # error - has_output > 0, but no data?
        return Connection.EOS

    while True:
        try:
            count = socket_obj.send(data)
            break
        except socket.timeout as e:
            LOG.debug("Socket timeout exception %s", str(e))
            raise  # caller must handle
        except socket.error as e:
            err = e.errno
            if err in [errno.EAGAIN,
                       errno.EWOULDBLOCK,
                       errno.EINTR]:
                # try again later
                return 0
            # else assume fatal let caller handle it:
            LOG.debug("Socket error exception %s", str(e))
            raise
        except Exception as e:  # beats me... assume fatal
            LOG.debug("unknown socket exception %s", str(e))
            raise

    if count > 0:
        connection.output_written(count)
    elif data:
        LOG.debug("Socket closed")
        count = Connection.EOS
        connection.close_output()
        connection.close_input()
    return count
def write_socket_output(connection, socket_obj):
    """Write data to the network layer.  Can support both blocking and
    non-blocking sockets.
    Returns the number of output bytes sent, or EOS if output processing
    is done.  Any exceptions raised by the socket are re-raised.
    """
    count = connection.has_output
    if count <= 0:
        return count  # 0 or EOS

    data = connection.output_data()
    if not data:
        # error - has_output > 0, but no data?
        return Connection.EOS

    while True:
        try:
            count = socket_obj.send(data)
            break
        except socket.timeout as e:
            LOG.debug("Socket timeout exception %s", str(e))
            raise  # caller must handle
        except socket.error as e:
            err = e.errno
            if err in [errno.EAGAIN,
                       errno.EWOULDBLOCK,
                       errno.EINTR]:
                # try again later
                return 0
            # else assume fatal let caller handle it:
            LOG.debug("Socket error exception %s", str(e))
            raise
        except Exception as e:  # beats me... assume fatal
            LOG.debug("unknown socket exception %s", str(e))
            raise

    if count > 0:
        connection.output_written(count)
    elif data:
        LOG.debug("Socket closed")
        count = Connection.EOS
        connection.close_output()
        connection.close_input()
    return count
Write data to the network layer.  Can support both blocking and
    non-blocking sockets.
    Returns the number of output bytes sent, or EOS if output processing
    is done.  Any exceptions raised by the socket are re-raised.