client_shell

Telnet client shell implementations for interactive terminal sessions.

class InputFilter(seq_xlat, byte_xlat, esc_delay=0.35)[source]

Translate terminal escape sequences and single bytes to retro encoding bytes.

Combines single-byte translation (backspace, delete) with multi-byte escape sequence matching (arrow keys, function keys). Uses prefix-based buffering inspired by blessed’s get_leading_prefixes to handle sequences split across reads.

When a partial match is buffered (e.g. a bare ESC), has_pending becomes True. The caller should start an esc_delay timer and call flush() if no further input arrives before the timer fires.

Parameters:
  • seq_xlat (Dict[bytes, bytes]) – Multi-byte escape sequence -> replacement bytes.

  • byte_xlat (Dict[int, int]) – Single input byte -> replacement byte.

  • esc_delay (float) – Seconds to wait before flushing a buffered prefix (default 0.35, matching blessed’s DEFAULT_ESCDELAY).

Initialize input filter with sequence and byte translation tables.

property has_pending: bool

Return True when the internal buffer holds a partial sequence.

flush()[source]

Flush buffered bytes, applying single-byte translation.

Called when the esc_delay timer fires without new input, meaning the buffered prefix is not a real escape sequence.

Return type:

bytes

Returns:

Translated bytes from the buffer (may be empty).

feed(data)[source]

Process input bytes, returning raw bytes to send to the remote host.

Escape sequences are matched against the configured table and replaced. Partial sequences are buffered until the next call. Single bytes are translated via the byte translation table.

Parameters:

data (bytes) – Raw bytes from terminal stdin.

Return type:

bytes

Returns:

Translated bytes ready to send to the remote BBS.

class TelnetTerminalShell[source]

Abstract base for telnet client terminal context managers.

Defines the interface used by _telnet_client_shell_impl and _raw_event_loop. Concrete implementations are Terminal (POSIX) and Terminal (Windows).

The type parameter _ModeT is the platform-specific terminal mode descriptor (a namedtuple). Subclasses bind it to their own concrete mode type so that set_mode() and _make_raw are type-safe within each platform.

abstractmethod async make_stdout()[source]

Return a writer for local terminal output.

Return type:

_StdoutWriter

abstractmethod setup_winch()[source]

Register a terminal resize handler.

Return type:

None

abstractmethod cleanup_winch()[source]

Deregister the terminal resize handler.

Return type:

None

abstractmethod async connect_stdin()[source]

Connect stdin to an asyncio StreamReader and return it.

Return type:

StreamReader

abstractmethod disconnect_stdin(reader)[source]

Disconnect the stdin pipe and signal EOF to reader.

Return type:

None

abstractmethod set_mode(mode)[source]

Apply terminal mode settings; a None mode is a no-op.

Return type:

None

abstractmethod check_auto_mode(switched_to_raw, last_will_echo)[source]

Check whether terminal mode should change mid-session.

Parameters:
  • switched_to_raw (bool) – Whether the terminal is already in raw mode.

  • last_will_echo (bool) – Previous value of the server’s WILL ECHO state.

Return type:

Optional[Tuple[bool, bool, bool]]

Returns:

(switched_to_raw, last_will_echo, local_echo) if a mode change is warranted, or None if no change is needed.

async telnet_client_shell(telnet_reader, telnet_writer)[source]

Minimal telnet client shell for POSIX terminals.

This shell performs minimal tty mode handling when a terminal is attached to standard in (keyboard), notably raw mode is often set and this shell may exit only by disconnect from server, or the escape character, ^].

stdin or stdout may also be a pipe or file, behaving much like nc(1).

Return type:

None