{"id":8686,"library":"standard-telnetlib","title":"Standard Telnetlib Redistribution","description":"standard-telnetlib is a redistribution of the `telnetlib` module, which was part of the Python standard library until its removal in Python 3.13 (deprecated in 3.11). It serves as a 'dead battery' backport, providing the original `telnetlib` functionality for projects that require it in Python versions where it's no longer built-in. This library allows developers to continue using the `telnetlib` client functionality without needing to pin older Python versions. The latest version is 3.13.0, released to coincide with Python 3.13's removal of the module.","status":"active","version":"3.13.0","language":"en","source_language":"en","source_url":"https://github.com/youknowone/python-deadlib","tags":["networking","telnet","legacy","compatibility","python3.13+"],"install":[{"cmd":"pip install standard-telnetlib","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"Despite being installed as 'standard-telnetlib', the package provides the original 'telnetlib' module, so the import path remains 'telnetlib'.","wrong":"from standard_telnetlib import Telnet","symbol":"Telnet","correct":"from telnetlib import Telnet"}],"quickstart":{"code":"import telnetlib\nimport getpass\nimport os\n\nHOST = os.environ.get('TELNET_HOST', 'localhost')\nPORT = int(os.environ.get('TELNET_PORT', 23))\nUSER = os.environ.get('TELNET_USER', 'testuser')\n\ntry:\n    # Establish connection\n    tn = telnetlib.Telnet(HOST, PORT, timeout=5)\n    \n    # Read until a login prompt, send username\n    tn.read_until(b'login: ', timeout=2)\n    tn.write(USER.encode('ascii') + b'\\n')\n    \n    # Read until password prompt, send password\n    if os.environ.get('TELNET_PASSWORD'): # Only prompt if env var is set\n        PASSWORD = getpass.getpass()\n        tn.read_until(b'Password: ', timeout=2)\n        tn.write(PASSWORD.encode('ascii') + b'\\n')\n    \n    # Example: Send a command and read output\n    tn.write(b'ls -l\\n')\n    output = tn.read_until(b'$ ', timeout=5) # Assuming a common shell prompt\n    print(output.decode('ascii'))\n    \n    tn.write(b'exit\\n')\n    print(tn.read_all().decode('ascii'))\n    \nexcept Exception as e:\n    print(f\"An error occurred: {e}\")\nfinally:\n    if 'tn' in locals() and tn:\n        tn.close()","lang":"python","description":"This quickstart demonstrates how to establish a Telnet connection, send login credentials, execute a command, read its output, and close the connection. Note that all data sent to and received from the Telnet server must be handled as bytes."},"warnings":[{"fix":"Install `standard-telnetlib` via `pip install standard-telnetlib` to make the module available on newer Python versions.","message":"The `telnetlib` module was removed from the Python standard library in Python 3.13 (after being deprecated in 3.11). While `standard-telnetlib` provides a backport, direct reliance on `telnetlib` without this redistribution will cause `ModuleNotFoundError` on Python 3.13 and newer.","severity":"breaking","affected_versions":"Python 3.13+"},{"fix":"For secure remote access, always prefer protocols like SSH (e.g., using Python libraries like `paramiko` or `netmiko`) which encrypt communication. Use Telnet only in highly controlled, isolated environments or for debugging non-sensitive services.","message":"The Telnet protocol transmits all data, including usernames and passwords, in cleartext. This makes it highly vulnerable to eavesdropping and session hijacking, and it is not suitable for secure communication over untrusted networks.","severity":"breaking","affected_versions":"All versions (inherent to Telnet protocol)"},{"fix":"Ensure all strings sent to `tn.write()` are encoded (e.g., `my_string.encode('ascii')`) and all bytes received from `tn.read_*()` methods are decoded (e.g., `my_bytes.decode('ascii')`) using the appropriate encoding for the remote system.","message":"All data written to the Telnet connection must be `bytes`, and all data read from the connection will be `bytes`. Incorrectly sending `str` or failing to decode received `bytes` will lead to `TypeError` or unexpected output.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always convert the port number to an integer before passing it to the `Telnet` constructor, e.g., `telnetlib.Telnet(host, int(port))`.","message":"The `port` argument to `telnetlib.Telnet()` constructor must be an integer. Passing a string will result in a `socket.gaierror` (e.g., `[Errno 10109] getaddrinfo failed`) or other connection errors.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Install the `standard-telnetlib` package: `pip install standard-telnetlib`.","cause":"The `telnetlib` module was removed from the Python standard library in Python 3.13.","error":"ModuleNotFoundError: No module named 'telnetlib'"},{"fix":"Encode the string to bytes before writing: `tn.write('my_command'.encode('ascii') + b'\\n')`.","cause":"Attempting to write a Python string object directly to the Telnet connection, which expects bytes.","error":"TypeError: a bytes-like object is required, not 'str'"},{"fix":"Ensure the `port` is an integer: `telnetlib.Telnet(HOST, int(PORT))`. Also, verify the `HOST` value is a correct IP address or resolvable hostname.","cause":"This error can occur if the `port` argument passed to `telnetlib.Telnet()` is a string instead of an integer, or if the `host` is unresolvable.","error":"socket.gaierror: [Errno 10109] getaddrinfo failed"},{"fix":"Check the remote Telnet server's status and logs. Increase the `timeout` parameter in `read_until()` calls. Ensure that the expected string passed to `read_until()` accurately matches the server's output, including case and trailing spaces/newlines.","cause":"The remote Telnet server closed the connection unexpectedly while the client was attempting to read data, or `read_until` could not find the expected string within the timeout.","error":"EOFError: Telnet connection closed"}]}