asyncutils.networking

Some asyncio protocols and a transport. See the asyncio documentation page.

Classes

CRLFProtocol

Carriage Return + Line Feed protocol for Windows.

CRProtocol

Carriage Return protocol. For legacy systems no longer officially supported by python, such as Mac OS 9, such that this will never be chosen as the default.

LFProtocol

Line Feed protocol for Unix-like systems.

LineProtocol

SocketTransport

A thread-unsafe transport that connects LineProtocol's to sockets.

Module Contents

class asyncutils.networking.CRLFProtocol[source]

Bases: LineProtocol

Carriage Return + Line Feed protocol for Windows.

class asyncutils.networking.CRProtocol[source]

Bases: LineProtocol

Carriage Return protocol. For legacy systems no longer officially supported by python, such as Mac OS 9, such that this will never be chosen as the default.

class asyncutils.networking.LFProtocol[source]

Bases: LineProtocol

Line Feed protocol for Unix-like systems.

class asyncutils.networking.LineProtocol[source]

Bases: asyncio.Protocol, asyncutils._internal.helpers.LoopMixinBase

An implementation of Protocol providing line-based buffering and writing. Not thread-safe.
The idea was originally introduced in PEP 3153, but did not see eventual adaptation in the standard library.
This particular implementation is designed to be used with SocketTransport, though other transports can enforce it too.
Instantiating this class will give an LFProtocol or CRLFProtocol depending on os.linesep.
_put_line(data: bytes) None[source]

Put the given data into the buffer as a single line.

close() bool[source]

Close the transport and return success.

connection_lost(exc: Exception | None) None[source]

Called when the connection is lost.

connection_made(transport: asyncio.WriteTransport) None[source]

Unlike the base class, this method does not take read-only transports.

data_received(data: bytes, bufsize: int = ...) None[source]

Called when some data is received, with data being a non-empty bytes object containing it.

async drain() None[source]

Wait until the transport is ready for more data to be written (i.e. the write buffer is flushed).

eof_received() None[source]

Called when the other end signals it won’t send any more data, for example by calling Transport.write_eof(), which closes the transport.

flush() None[source]

Flush the internal buffer and put in remaining data as a single line.

pause_writing() None[source]

Called when the transport’s buffer goes over the high watermark.

async read_line() str | None[source]

Read a line from the internal buffer and return it, or None if EOF is reached.

resume_writing() None[source]

Called when the transport’s buffer drains below the low watermark.

signal_eof() None[source]

Signal that the stream is at EOF.

write_line(line: str) None[source]

Write the string line to the transport, followed by the newline sequence.

async write_line_with_backpressure(line: str) None[source]

Write the string line to the transport, followed by the newline sequence, after draining it.

write_literal(data: bytes) None[source]

Write the given bytes into the transport without appending a newline.

async write_literal_with_backpressure(data: bytes) None[source]

Write the given bytes into the transport without appending a newline, after draining it.

CARRIAGE_RETURN: ClassVar[bytes]

The carriage return sequence used by this protocol as bytes.

NEWLINE: ClassVar[bytes]

The newline sequence used by this protocol as bytes.

property connected_transport: asyncio.WriteTransport

The transport associated with this protocol; raises ConnectionError if not connected.

property transport: asyncio.WriteTransport | None

The transport associated with this protocol, or None if not connected.

class asyncutils.networking.SocketTransport(sock: socket.socket | None = ...)[source]

Bases: asyncio.Transport

A thread-unsafe transport that connects LineProtocol’s to sockets.

Initialize the transport, connecting the socket immediately if given.

_reset_extra() None[source]
abort() None[source]

Close the transport immediately, without waiting for pending operations to complete.

can_write_eof() Literal[True][source]

Stub implementation that always returns True.

close(e: Exception | None = ...) None[source]

Close the transport and flush the outgoing data buffer. No more data will be received. After all buffered data is flushed, the protocol’s connection_lost() method will be called with None as argument. The transport is not to be used once closed.

connect_sock(sock: socket.socket = ...) None[source]

Connect the transport to the given socket.

disconnect_sock() socket.socket | None[source]

Disconnect the transport from its socket and return it, or None if not connected.

get_protocol() LineProtocol[source]

Return the current protocol. For this class, it is expected to always be a LineProtocol or one of its subclasses.

get_write_buffer_limits() tuple[int, int][source]

Get the high and low watermarks for write flow control. Return a tuple (low, high), where low and high are positive number of bytes.

get_write_buffer_size() int[source]

Return the current size of the output buffer used by the transport.

is_closing() bool[source]

Return whether the transport is closing or already closed.

static make_protocol() LineProtocol[source]

Return a new protocol compatible with this transport. The default implementation returns a LineProtocol, so if overriding this in subclasses, remember to add # type: ignore[override] comments as appropriate.

set_write_buffer_limits(high: int | None = ..., low: int | None = ...) None[source]

Set the high and low watermarks for write flow control in bytes.

sock_context(sock: socket.socket) asyncutils._internal.types.DualContextManager[None][source]

Return a context manager, both sync and async, that connects the transport to the given socket on entry and disconnects it on exit.

write(data: collections.abc.Iterable[int]) None[source]

Write the given iterable over integers in range(256) interpreted as characters into the transport.

write_eof() None[source]

Close the write end of the transport after flushing all buffered data. Data may still be received.

property loop: asyncio.AbstractEventLoop

Override this if the type of the protocols this transport accepts is altered in subclasses.