server_pty_shell

PTY shell implementation for telnetlib3.

This module provides the ability to spawn PTY-connected programs (bash, tmux, nethack, etc.) for each telnet connection, with proper terminal negotiation forwarding.

make_pty_shell(program, args=None, preexec_fn=None, raw_mode=False)[source]

Factory returning a shell callback for PTY execution.

Parameters:
  • program (str) – Path to program to execute.

  • args (Optional[List[str]]) – List of arguments for the program.

  • preexec_fn (Optional[Callable[[], None]]) – Optional callable to run in child before exec. Useful for test coverage tracking in the forked child process.

  • raw_mode (bool) – If True, disable PTY echo and canonical mode. Use for programs that handle their own terminal I/O (e.g., blessed, curses, ucs-detect).

Return type:

Callable[[Union[TelnetReader, TelnetReaderUnicode], Union[TelnetWriter, TelnetWriterUnicode]], Awaitable[None]]

Returns:

Async shell callback suitable for use with create_server().

Example usage:

from telnetlib3 import create_server, make_pty_shell

server = await create_server(
    host='localhost',
    port=6023,
    shell=make_pty_shell('/bin/bash', ['-l'])
)
async pty_shell(reader, writer, program, args=None, preexec_fn=None, raw_mode=False)[source]

PTY shell callback for telnet server.

Parameters:
Return type:

Optional[int]

Returns:

Child process exit code, or None if unknown.

exception PTYSpawnError[source]

Raised when PTY child process fails to exec.