client

Telnet Client API for the ‘telnetlib3’ python package.

class TelnetClient(term='unknown', cols=80, rows=25, tspeed=(38400, 38400), xdisploc='', send_environ=None, shell=None, encoding='utf8', encoding_errors='strict', force_binary=False, connect_minwait=0, connect_maxwait=4.0, compression=None, limit=None, waiter_closed=None, _waiter_connected=None, gmcp_modules=None)[source]

Telnet client that supports all common options.

Useful for automation, appearing as a virtual terminal to the remote end without requiring an interactive terminal to run.

Initialize TelnetClient with terminal parameters.

DEFAULT_LOCALE = 'en_US'

On send_env(), the value of ‘LANG’ will be ‘C’ for binary transmission. When encoding is specified (utf8 by default), the LANG variable must also contain a locale, this value is used, providing a full default LANG value of ‘en_US.utf8’

DEFAULT_SEND_ENVIRON = ('TERM', 'LANG', 'COLUMNS', 'LINES', 'COLORTERM')

Default environment variables to send via NEW_ENVIRON

connection_made(transport)[source]

Handle connection made to server.

Wire up telnet option callbacks for terminal type, speed, display, environment, window size, and character set negotiation.

Return type:

None

on_gmcp(package, data)[source]

Store incoming GMCP data on writer.ctx, merging dict updates.

Return type:

None

send_ttype()[source]

Callback for responding to TTYPE requests.

Return type:

str

send_tspeed()[source]

Callback for responding to TSPEED requests.

Return type:

Tuple[int, int]

send_xdisploc()[source]

Callback for responding to XDISPLOC requests.

Return type:

str

send_env(keys)[source]

Callback for responding to NEW_ENVIRON requests.

Only sends variables listed in _send_environ (set via send_environ parameter or --send-environ CLI option).

Parameters:

keys (Sequence[str]) – Values are requested for the keys specified. When empty, all environment values that wish to be volunteered should be returned.

Return type:

Dict[str, Any]

Returns:

Environment values requested, or an empty string for keys not available. A return value must be given for each key requested.

send_charset(offered)[source]

Callback for responding to CHARSET requests.

Simplified policy:

  • If client has explicit encoding that matches an offered charset, use it

  • If client has explicit encoding that isn’t offered,

    • For Latin-1 (weak default), accept first viable offered encoding

    • For other explicit encodings, reject (keep client’s choice)

  • If no explicit encoding preference, accept first viable offered encoding

  • If no viable encodings found, reject

Parameters:

offered (List[str]) – CHARSET options offered by server.

Return type:

str

Returns:

Character encoding agreed to be used, or empty string to reject.

on_request_charset()[source]

Offer callback for client-initiated CHARSET REQUEST, RFC 2066.

Called by request_charset() to determine which character sets the client offers to the server.

Return type:

List[str]

Returns:

List of charset name strings to offer.

on_request_environ()[source]

Offer callback for client-initiated NEW_ENVIRON SEND, RFC 1572.

Called by request_environ() to determine which environment variable names the client requests from the server.

Return type:

List[str]

Returns:

List of environment variable names to request.

send_naws()[source]

Callback for responding to NAWS requests.

Return type:

Tuple[int, int]

Returns:

Client window size as (rows, columns).

encoding(outgoing=None, incoming=None)[source]

Return encoding for the given stream direction.

Parameters:
  • outgoing (Optional[bool]) – Whether the return value is suitable for encoding bytes for transmission to server.

  • incoming (Optional[bool]) – Whether the return value is suitable for decoding bytes received by the client.

Raises:

TypeError – When a direction argument, either outgoing or incoming, was not set True.

Return type:

str

Returns:

'US-ASCII' for the directions indicated, unless BINARY RFC 856 has been negotiated for the direction indicated or force_binary is set True.

class TelnetTerminalClient(term='unknown', cols=80, rows=25, tspeed=(38400, 38400), xdisploc='', send_environ=None, shell=None, encoding='utf8', encoding_errors='strict', force_binary=False, connect_minwait=0, connect_maxwait=4.0, compression=None, limit=None, waiter_closed=None, _waiter_connected=None, gmcp_modules=None)[source]

Telnet client for sessions with a network virtual terminal (NVT).

Initialize TelnetClient with terminal parameters.

send_naws()[source]

Callback replies to request for window size, NAWS RFC 1073.

Return type:

Tuple[int, int]

Returns:

Window dimensions by lines and columns.

send_env(keys)[source]

Callback replies to request for env values, NEW_ENVIRON RFC 1572.

Return type:

Dict[str, Any]

Returns:

Super class value updated with window LINES and COLUMNS.

async open_connection(host=None, port=23, *, client_factory=None, family=0, flags=0, local_addr=None, encoding='utf8', encoding_errors='replace', force_binary=False, term='unknown', cols=80, rows=25, tspeed=(38400, 38400), xdisploc='', shell=None, connect_minwait=0, connect_maxwait=3.0, connect_timeout=None, compression=None, waiter_closed=None, _waiter_connected=None, limit=None, send_environ=None, ssl=None, server_hostname=None)[source]

Connect to a TCP Telnet server as a Telnet client.

Parameters:
  • host (Optional[str]) – Remote Internet TCP Server host.

  • port (int) – Remote Internet host TCP port.

  • client_factory (Optional[Callable[..., BaseClient]]) – Client connection class factory. When None, TelnetTerminalClient is used when stdin is attached to a terminal, TelnetClient otherwise.

  • family (int) – Same meaning as asyncio.loop.create_connection().

  • flags (int) – Same meaning as asyncio.loop.create_connection().

  • local_addr (Optional[Tuple[str, int]]) – Same meaning as asyncio.loop.create_connection().

  • encoding (Union[str, bool]) –

    The default assumed encoding, or False to disable unicode support. This value is used for decoding bytes received by and encoding bytes transmitted to the Server. These values are preferred in response to NEW_ENVIRON RFC 1572 as environment value LANG, and by CHARSET RFC 2066 negotiation.

    The server’s attached reader, writer streams accept and return unicode, unless this value is explicitly set False. In that case, the attached streams interfaces are bytes-only.

  • encoding_errors (str) – Same meaning as codecs.Codec.encode().

  • term (str) – Terminal type sent for requests of TTYPE, RFC 930 or as Environment value TERM by NEW_ENVIRON negotiation, RFC 1672.

  • cols (int) – Client window dimension sent as Environment value COLUMNS by NEW_ENVIRON negotiation, RFC 1672 or NAWS RFC 1073.

  • rows (int) – Client window dimension sent as Environment value LINES by NEW_ENVIRON negotiation, RFC 1672 or NAWS RFC 1073.

  • tspeed (Tuple[int, int]) – Client BPS line speed in form (rx, tx) for receive and transmit, respectively. Sent when requested by TSPEED, RFC 1079.

  • xdisploc (str) – String transmitted in response for request of XDISPLOC, RFC 1086 by server (X11).

  • connect_minwait (float) –

    The client allows any additional telnet negotiations to be demanded by the server within this period of time before launching the shell. Servers should assert desired negotiation on-connect and in response to 1 or 2 round trips.

    A server that does not make any telnet demands, such as a TCP server that is not a telnet server, will delay the execution of shell for exactly this amount of time.

  • connect_maxwait (float) – If the remote end is not compliant, or otherwise confused by our demands, the shell continues anyway after the greater of this value has elapsed. A client that is not answering option negotiation will delay the start of the shell by this amount.

  • connect_timeout (Optional[float]) – Timeout in seconds for the TCP connection to be established. When None (default), no timeout is applied and the connection attempt may block indefinitely. When specified, a ConnectionError is raised if the connection is not established within the given time.

  • compression (Optional[bool]) – MCCP compression policy. None (default) passively accepts compression when offered by the server. True actively requests MCCP2/MCCP3. False rejects all compression offers.

  • force_binary (bool) – When True, the encoding is used regardless of BINARY mode negotiation.

  • waiter_closed (Optional[Future[None]]) – Future that completes when the connection is closed.

  • shell (Optional[Callable[[Union[TelnetReader, TelnetReaderUnicode], Union[TelnetWriter, TelnetWriterUnicode]], Coroutine[Any, Any, None]]]) – An async function that is called after negotiation completes, receiving arguments (reader, writer).

  • limit (Optional[int]) – The buffer limit for reader stream.

  • ssl (Union[bool, SSLContext, None]) – TLS configuration. True creates a default ssl.create_default_context() that verifies CA certificates. An ssl.SSLContext gives full control. None (default) uses plain TCP.

  • server_hostname (Optional[str]) – Hostname for TLS certificate verification. When ssl is truthy and server_hostname is None, defaults to host.

Return type:

Tuple[Union[TelnetReader, TelnetReaderUnicode], Union[TelnetWriter, TelnetWriterUnicode]]

Returns:

The reader is a TelnetReader instance, the writer is a TelnetWriter instance.