stream_reader

Module provides class TelnetReader and TelnetReaderUnicode.

class TelnetReader(limit=65536, loop=None)

Bases: asyncio.streams.StreamReader

A reader interface for the telnet protocol.

at_eof()

Return True if the buffer is empty and ‘feed_eof’ was called.

exception()
feed_data(data)
feed_eof()
read(n=-1)

Read up to n bytes from the stream.

If n is not provided, or set to -1, read until EOF and return all read bytes. If the EOF was received and the internal buffer is empty, return an empty bytes object.

If n is zero, return empty bytes object immediatelly.

If n is positive, this function try to read n bytes, and may return less or equal bytes than requested, but at least one byte. If EOF was received before any byte is read, this function returns empty byte object.

Returned value is not limited with limit, configured at stream creation.

If stream was paused, this function will automatically resume it if needed.

readexactly(n)

Read exactly n bytes.

Raise an IncompleteReadError if EOF is reached before n bytes can be read. The IncompleteReadError.partial attribute of the exception will contain the partial read bytes.

if n is zero, return empty bytes object.

Returned value is not limited with limit, configured at stream creation.

If stream was paused, this function will automatically resume it if needed.

readline()

Read one line.

Where “line” is a sequence of characters ending with CR LF, LF, or CR NUL. This readline function is a strict interpretation of Telnet Protocol RFC 854.

The sequence “CR LF” must be treated as a single “new line” character and used whenever their combined action is intended; The sequence “CR NUL” must be used where a carriage return alone is actually desired; and the CR character must be avoided in other contexts.

And therefor, a line does not yield for a stream containing a CR if it is not succeeded by NUL or LF.

Given stream readline() yields
--\r\x00--- --\r, --- ...
--\r\n--- --\r\n, --- ...
--\n--- --\n, --- ...
--\r--- --\r, --- ...

If EOF is received before the termination of a line, the method will yield the partially read string.

This method is a coroutine().

readuntil(separator=b'\n')

Read data from the stream until separator is found.

On success, the data and separator will be removed from the internal buffer (consumed). Returned data will include the separator at the end.

Configured stream limit is used to check result. Limit sets the maximal length of data that can be returned, not counting the separator.

If an EOF occurs and the complete separator is still not found, an IncompleteReadError exception will be raised, and the internal buffer will be reset. The IncompleteReadError.partial attribute may contain the separator partially.

If the data cannot be read because of over limit, a LimitOverrunError exception will be raised, and the data will be left in the internal buffer, so it can be read again.

set_exception(exc)
set_transport(transport)
class TelnetReaderUnicode(fn_encoding, *, limit=65536, loop=None, encoding_errors='replace')

Bases: telnetlib3.stream_reader.TelnetReader

A Unicode StreamReader interface for Telnet protocol.

Parameters:fn_encoding (callable) – function callback, receiving boolean keyword argument, incoming=True, which is used by the callback to determine what encoding should be used to decode the value in the direction specified.
at_eof()

Return True if the buffer is empty and ‘feed_eof’ was called.

decode(buf, final=False)

Decode bytes buf using preferred encoding.

exception()
feed_data(data)
feed_eof()
read(n=-1)

Read up to n bytes.

If the EOF was received and the internal buffer is empty, return an empty string.

Parameters:n (int) – If n is not provided, or set to -1, read until EOF and return all characters as one large string.
Return type:str

This method is a coroutine().

readexactly(n)

Read exactly n unicode characters.

Raises:asyncio.IncompleteReadError – if the end of the stream is reached before n can be read. the asyncio.IncompleteReadError.partial attribute of the exception contains the partial read characters.
Return type:str

This method is a coroutine().

readline()

Read one line.

See ancestor method, readline() for details.

This method is a coroutine().

readuntil(separator=b'\n')

Read data from the stream until separator is found.

On success, the data and separator will be removed from the internal buffer (consumed). Returned data will include the separator at the end.

Configured stream limit is used to check result. Limit sets the maximal length of data that can be returned, not counting the separator.

If an EOF occurs and the complete separator is still not found, an IncompleteReadError exception will be raised, and the internal buffer will be reset. The IncompleteReadError.partial attribute may contain the separator partially.

If the data cannot be read because of over limit, a LimitOverrunError exception will be raised, and the data will be left in the internal buffer, so it can be read again.

set_exception(exc)
set_transport(transport)