sync
Synchronous (blocking) interface for telnetlib3.
This module provides a non-asyncio interface that wraps the async telnetlib3 implementation. The asyncio event loop runs in a background thread, and blocking methods wait on thread-safe futures.
Example client usage:
from telnetlib3.sync import TelnetConnection
with TelnetConnection('localhost', 6023) as conn:
conn.write('hello\r\n')
print(conn.readline())
Example server usage:
from telnetlib3.sync import BlockingTelnetServer
import threading
def handler(conn):
conn.write('Hello!\r\n')
while line := conn.readline():
conn.write(f'Echo: {line}')
server = BlockingTelnetServer('localhost', 6023, handler=handler)
server.serve_forever()
- class TelnetConnection(host, port=23, timeout=None, encoding='utf8', **kwargs)[source]
Bases:
objectBlocking telnet client connection.
Wraps async
telnetlib3.open_connection()with blocking methods. The asyncio event loop runs in a daemon thread.- Parameters:
host (
str) – Remote server hostname or IP address.port (
int) – Remote server port (default 23).timeout (
Optional[float]) – Default timeout for operations in seconds.encoding (
str) – Character encoding (default ‘utf8’).connect_timeout – Timeout in seconds for the TCP connection to be established. Passed to
telnetlib3.open_connection().kwargs (
Any) – Additional arguments passed totelnetlib3.open_connection().
Example:
with TelnetConnection('localhost', 6023) as conn: conn.write('hello\r\n') response = conn.readline()
Initialize connection parameters without connecting.
- connect()[source]
Establish connection to the server.
Blocks until connected or timeout expires.
- Raises:
RuntimeError – If already connected.
TimeoutError – If connection times out.
ConnectionError – If connection fails.
Exception – If connection fails for other reasons.
- Return type:
- read(n=-1, timeout=None)[source]
Read up to n bytes/characters from the connection.
Blocks until data is available or timeout expires.
- Parameters:
- Return type:
- Returns:
Data read from connection.
- Raises:
TimeoutError – If timeout expires before data available.
EOFError – If connection closed.
- read_some(timeout=None)[source]
Read some available data from the connection.
Unlike
read()withn=-1, this returns as soon as any data is available rather than waiting for EOF.
- readline(timeout=None)[source]
Read one line from the connection.
Blocks until a complete line is received or timeout expires.
- read_until(match, timeout=None)[source]
Read until match is found.
Like old telnetlib’s read_until method.
- Parameters:
- Return type:
- Returns:
Data up to and including match.
- Raises:
TimeoutError – If timeout expires before match found.
EOFError – If connection closed before match found.
- write(data)[source]
Write data to the connection.
This method buffers data and returns immediately. Use
flush()to ensure data is sent.
- flush(timeout=None)[source]
Flush buffered data to the connection.
Blocks until all buffered data has been sent.
- Parameters:
timeout (
Optional[float]) – Timeout in seconds (uses default if None).- Raises:
TimeoutError – If timeout expires.
- Return type:
- get_extra_info(name, default=None)[source]
Get extra information about the connection.
After negotiation completes, provides access to negotiated values:
'TERM': Terminal type (e.g., ‘xterm-256color’)'cols': Terminal width in columns'rows': Terminal height in rows'peername': Remote address tuple (host, port)'LANG': Language/locale setting
- wait_for(remote=None, local=None, pending=None, timeout=None)[source]
Wait for telnet option negotiation states.
This method blocks until the specified options reach their desired states, or timeout expires. This is not possible with the legacy telnetlib module.
- Parameters:
remote (
Optional[dict[str,bool]]) – Dict of options for remote (client WILL) state. Example:{'NAWS': True, 'TTYPE': True}local (
Optional[dict[str,bool]]) – Dict of options for local (client DO) state. Example:{'BINARY': True, 'ECHO': True}pending (
Optional[dict[str,bool]]) – Dict of options for pending negotiation state. Example:{'TTYPE': False}(wait for negotiation to complete)timeout (
Optional[float]) – Timeout in seconds (uses default if None).
- Raises:
TimeoutError – If timeout expires before conditions met.
- Return type:
Example - wait for terminal info before proceeding:
conn = TelnetConnection('localhost', 6023) conn.connect() # Wait for NAWS and TTYPE negotiation to complete conn.wait_for(remote={'NAWS': True, 'TTYPE': True}, timeout=5.0) # Now terminal info is available term = conn.get_extra_info('TERM') cols = conn.get_extra_info('cols') rows = conn.get_extra_info('rows') print(f"Terminal: {term} ({cols}x{rows})")
- property writer: TelnetWriter
Access the underlying TelnetWriter for advanced operations.
This provides access to telnet protocol features not available in the legacy telnetlib:
Option state inspection (
writer.remote_option,writer.local_option)Mode detection (
writer.mode- ‘local’, ‘remote’, ‘kludge’)Protocol constants and negotiation methods
- Returns:
The underlying TelnetWriter instance.
- class BlockingTelnetServer(host='localhost', port=6023, handler=None, **kwargs)[source]
Bases:
objectBlocking telnet server.
Wraps async
telnetlib3.create_server()with a blocking interface. Each client connection can be handled in a separate thread.- Parameters:
host (
str) – Address to bind to.port (
int) – Port to bind to (default 6023).handler (
Optional[Callable[[ServerConnection],None]]) – Function called for each client connection. Receives aTelnetConnection-like object as argument.kwargs (
Any) – Additional arguments passed totelnetlib3.create_server().
Example with handler:
def handle_client(conn): conn.write('Welcome!\r\n') while line := conn.readline(): conn.write(f'Echo: {line}') server = BlockingTelnetServer('localhost', 6023, handler=handle_client) server.serve_forever()
Example with manual accept loop:
server = BlockingTelnetServer('localhost', 6023) server.start() while True: conn = server.accept() threading.Thread(target=handle_client, args=(conn,)).start()
Initialize server parameters without starting.
- start()[source]
Start the server.
Non-blocking. Use
accept()orserve_forever()to handle clients.- Raises:
RuntimeError – If already started.
- Return type:
- accept(timeout=None)[source]
Accept a client connection.
Blocks until a client connects.
- Parameters:
timeout (
Optional[float]) – Timeout in seconds (None for no timeout).- Return type:
- Returns:
Connection object for the client.
- Raises:
TimeoutError – If timeout expires.
RuntimeError – If server not started.
- serve_forever()[source]
Serve clients forever.
Blocks and handles each client in a new thread using the handler function provided at construction.
- Raises:
RuntimeError – If no handler was provided.
- Return type:
- class ServerConnection(reader, writer, loop)[source]
Bases:
objectBlocking interface for a server-side client connection.
This is similar to
TelnetConnectionbut for server-side use. Created automatically when a client connects toBlockingTelnetServer.Provides miniboa-compatible properties for easier migration:
active- Connection state (set to False to disconnect)terminal_type,columns,rows- Terminal infoaddrport()- Returns “IP:PORT” stringidle(),duration()- Timing informationdeactivate()- Set active=False to queue disconnection
Initialize connection from reader/writer pair.
- read(n=-1, timeout=None)[source]
Read up to n bytes/characters from the connection.
- Parameters:
- Return type:
- Returns:
Data read from connection.
- Raises:
RuntimeError – If connection already closed.
TimeoutError – If timeout expires.
EOFError – If connection closed.
- read_some(timeout=None)[source]
Read some available data from the connection.
Unlike
read()withn=-1, this returns as soon as any data is available rather than waiting for EOF.
- readline(timeout=None)[source]
Read one line from the connection.
- Parameters:
- Return type:
- Returns:
Line including terminator.
- Raises:
RuntimeError – If connection already closed.
TimeoutError – If timeout expires.
EOFError – If connection closed.
- read_until(match, timeout=None)[source]
Read until match is found.
- Parameters:
- Return type:
- Returns:
Data up to and including match.
- Raises:
RuntimeError – If connection already closed.
TimeoutError – If timeout expires.
EOFError – If connection closed.
- write(data)[source]
Write data to the connection.
- Parameters:
- Raises:
RuntimeError – If connection already closed.
- Return type:
- flush(timeout=None)[source]
Flush buffered data to the connection.
- Parameters:
- Raises:
RuntimeError – If connection already closed.
TimeoutError – If timeout expires.
- Return type:
- get_extra_info(name, default=None)[source]
Get extra information about the connection.
After negotiation completes, provides access to negotiated values:
'TERM': Terminal type (e.g., ‘xterm-256color’)'cols': Terminal width in columns'rows': Terminal height in rows'peername': Remote address tuple (host, port)
- wait_for(remote=None, local=None, pending=None, timeout=None)[source]
Wait for telnet option negotiation states.
Blocks until the specified options reach their desired states.
- Parameters:
- Raises:
RuntimeError – If connection already closed.
TimeoutError – If timeout expires.
- Return type:
Example:
conn = server.accept() conn.wait_for(remote={'NAWS': True}, timeout=5.0) print(f"Window: {conn.columns}x{conn.rows}")
- property writer: TelnetWriter
Access the underlying TelnetWriter for advanced operations.
- Returns:
The underlying TelnetWriter instance.
- property active: bool
Connection health status (miniboa-compatible).
Set to False to disconnect on next opportunity.
- send(text)[source]
Send text to the client (miniboa-compatible).
Alias for
write(). Normalizes newlines to rn like miniboa.
- addrport()[source]
Return client’s IP:PORT as string (miniboa-compatible).
- Return type:
- Returns:
String in format “IP:PORT”.
- idle()[source]
Seconds since last input received (miniboa-compatible).
- Return type:
- Returns:
Idle time in seconds.