stream_reader

Module provides class TelnetReader and TelnetReaderUnicode.

class TelnetReader(limit=65536)[source]

Bases: object

This is a copy of asyncio.StreamReader, with a little care for telnet-like readline(), and something about _waiter which I don’t really

exception()[source]
set_exception(exc)[source]
set_transport(transport)[source]
feed_eof()[source]
at_eof()[source]

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

feed_data(data)[source]
connection_closed
close()[source]
read(n=-1)[source]

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 immediately.

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)[source]

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()[source]

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.

readuntil(separator=b'\n')[source]

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.

class TelnetReaderUnicode(fn_encoding, *, limit=65536, encoding_errors='replace')[source]

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.

close()
connection_closed
exception()
feed_data(data)
feed_eof()
read(n=-1)[source]

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
readexactly(n)[source]

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
readline()[source]

Read one line.

See ancestor method, readline() for details.

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)
decode(buf, final=False)[source]

Decode bytes buf using preferred encoding.