herosdevices.core.bus.telnet.telnet

This module provides a class for managing telnet connections and a context manager for handling telnet sessions.

Module Contents

herosdevices.core.bus.telnet.telnet.TELNET_DELAYS_DEFAULT: dict
class herosdevices.core.bus.telnet.telnet.TelnetConnection(address: str, port: int = 23, timeout: float = 1.0, line_termination: bytes = b'\n', keep_alive: bool = True, delays: dict | None = None)[source]

A class to manage telnet communication connections.

This class provides functionality to handle telnet connections including opening/closing connections, reading data, and writing data.

Parameters:
  • address – The address of the telnet socket, something like /dev/ttyUSB0.

  • port – Port the telnet server is listening on

  • line_termination – character that terminates a line in the communication.

  • keep_alive – Flag indicating whether to keep the connection open between operations.

  • delays

    Dictionary containing delay times for in between telnet operations. Default telnet delays for telnet devices. Available keys are:

    • ”write”: Time to wait after writing a command to the device.

    • ”read_echo”: Time to wait before reading a response from the device.

    herosdevices.core.bus.telnet.TELNET_DELAYS_DEFAULT sets the default delays.

address
port = 23
timeout = 1.0
line_termination = b'\n'
connection
keep_alive = True
delays
check_alive() bool[source]

Check if the telnet connection is alive.

operation() collections.abc.Iterator[None][source]

Context manager for handling telnet connection operations.

Ensures the telnet connection is open before performing operations and closes it afterward if self.keep_alive is False.

Yields:

Yields control back to the caller for performing operations within the context.

wait(operation: str) None[source]

Introduce a (synchronous) delay based on the specified operation type.

Parameters:

operation – The operation type. For possible types see TELNET_DELAYS_DEFAULT.

read() str | None[source]

Read all available data from the telnet connection and decodes it into a string.

Returns:

The decoded data as string, or None if an error occurs.

read_line() str | None[source]

Read a single line from the telnet connection.

Returns:

The decoded line as string, or None if an error occurs.

write(message: str, read_echo: Literal[True] = True, read_line: bool = True) str[source]
write(message: str, read_echo: Literal[False], read_line: bool = True) None
write(message: str, read_echo: bool = False, read_line: bool = True) str | None

Write a message to the telnet connection.

Parameters:
  • message – The message to be written to the telnet connection.

  • read_echo – If True, reads back the echo after writing. Defaults to False.

  • read_line – If True, data is read until self.line_termination occurs in the data. Otherwise all available data is read.

Returns:

If read_echo is True, returns the echo read from the connection as string; otherwise returns None.