Telnet Client API for the ‘telnetlib3’ python package.

class TelnetClient(term='unknown', cols=80, rows=25, tspeed=(38400, 38400), xdisploc='', *args, **kwargs)

Bases: telnetlib3.client_base.BaseClient

Telnet client that supports all common options.

This class is useful for automation, it appears to be a virtual terminal to the remote end, but does not require an interactive terminal to run.


Begin on-connect negotiation.

A Telnet client is expected to send only a minimal amount of client session options immediately after connection, it is generally the server which dictates server option support.

Deriving implementations should always call super().begin_negotiation().


Callback, return whether negotiation is complete.

Parameters:final (bool) – Whether this is the final time this callback will be requested to answer regarding protocol negotiation.
Returns:Whether negotiation is over (client end is satisfied).
Return type:bool

Method is called on each new command byte processed until negotiation is considered final, or after connect_maxwait has elapsed, setting _waiter_connected to value self when complete.

This method returns False until connect_minwait has elapsed, ensuring the server may batch telnet negotiation demands without prematurely entering the callback shell.

Ensure super().check_negotiation() is called and conditionally combined when derived.


Called when the connection is lost or closed.

Parameters:exc (Exception) – exception. None indicates a closing EOF sent by this end.

Callback for connection made to server.


Process bytes received by transport.


Time elapsed since client connected, in seconds as float.

encoding(outgoing=None, incoming=None)

Return encoding for the given stream direction.

  • outgoing (bool) – Whether the return value is suitable for encoding bytes for transmission to server.
  • incoming (bool) – Whether the return value is suitable for decoding bytes received by the client.

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


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

Return type:


Value resolution order (first-matching):

  • value set by set_encoding().
  • value of get_extra_info() using key argument, lang.
  • value of default_encoding.
  • US-ASCII when binary transmission not allowed.

Called when the other end calls write_eof() or equivalent.

get_extra_info(name, default=None)

Get optional client protocol or transport information.


Time elapsed since data last received, in seconds as float.


Callback for responding to CHARSET requests.

Receives a list of character encodings offered by the server as offered such as ('LATIN-1', 'UTF-8'), for which the client may return a value agreed to use, or None to disagree to any available offers. Server offerings may be encodings or codepages.

The default implementation selects any matching encoding that python is capable of using, preferring any that matches self.encoding if matched in the offered list.

Parameters:offered (list) – list of CHARSET options offered by server.
Returns:character encoding agreed to be used.
Return type:str or None.

Callback for responding to NEW_ENVIRON requests.

Parameters:keys (dict) – Values are requested for the keys specified. When empty, all environment values that wish to be volunteered should be returned.
Returns:dictionary of environment values requested, or an empty string for keys not available. A return value must be given for each key requested.
Return type:dict[(key, value), .]

Callback for responding to NAWS requests.

Return type:(int, int)
Returns:client window size as (rows, columns).

Callback for responding to TSPEED requests.


Callback for responding to TTYPE requests.


Callback for responding to XDISPLOC requests.

open_connection(host=None, port=23, *, client_factory=None, loop=None, family=0, flags=0, local_addr=None, log=None, encoding='utf8', encoding_errors='replace', force_binary=False, term='unknown', cols=80, rows=25, tspeed=(38400, 38400), xdisploc='', shell=None, connect_minwait=2.0, connect_maxwait=3.0, waiter_closed=None, _waiter_connected=None, limit=None)

Connect to a TCP Telnet server as a Telnet client.

  • host (str) – Remote Internet TCP Server host.
  • port (int) – Remote Internet host TCP port.
  • client_factory (client_base.BaseClient) – Client connection class factory. When None, TelnetTerminalClient is used when stdin is attached to a terminal, TelnetClient otherwise.
  • loop (asyncio.base_events.BaseEventLoop) – set the event loop to use. The return value of asyncio.get_event_loop() is used when unset.
  • family (int) – Same meaning as asyncio.BaseEventLoop.create_connection().
  • flags (int) – Same meaning as asyncio.BaseEventLoop.create_connection().
  • local_addr (tuple) – Same meaning as asyncio.BaseEventLoop.create_connection().
  • log (logging.Logger) – target logger, if None is given, one is created using the namespace 'telnetlib3.server'.
  • encoding (str) –

    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 explicitly set False. In that case, the attached streams interfaces are bytes-only.

  • 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) – Tuple of 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).
  • shell (asyncio.coroutine) – A coroutine that is called after negotiation completes, receiving arguments (reader, writer). The reader is a TelnetStreamReader instance, the writer is a TelnetStreamWriter instance.
  • 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 complaint, or otherwise confused by our demands and failing to reply to pending negotiations, the shell continues anyway after the greater of this value or connect_minwait elapsed.
  • force_binary (bool) – When True, the encoding specified is used for both directions even when failing BINARY negotiation, RFC 856. This parameter has no effect when encoding=False.
  • encoding_errors (str) – Same meaning as codecs.Codec.
  • connect_minwait – XXX
  • connect_maxwait – If the remote end is not complaint, 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.
  • limit (int) – The buffer limit for reader stream.
Return (reader, writer):

The reader is a TelnetStreamReader instance, the writer is a TelnetStreamWriter instance.

This function is a coroutine().