Install & Compatibility
Where this runs
tested against v3.13.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.018s · 17.8MB
glibcpy 3.10–3.920 runs
installs and imports cleanly · install 1.5s · import 0.014s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Telnet
✓ from telnetlib import Telnet
✗ from standard_telnetlib import Telnet
Despite being installed as 'standard-telnetlib', the package provides the original 'telnetlib' module, so the import path remains 'telnetlib'.
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.
import telnetlib
import getpass
import os
HOST = os.environ.get('TELNET_HOST', 'localhost')
PORT = int(os.environ.get('TELNET_PORT', 23))
USER = os.environ.get('TELNET_USER', 'testuser')
try:
# Establish connection
tn = telnetlib.Telnet(HOST, PORT, timeout=5)
# Read until a login prompt, send username
tn.read_until(b'login: ', timeout=2)
tn.write(USER.encode('ascii') + b'\n')
# Read until password prompt, send password
if os.environ.get('TELNET_PASSWORD'): # Only prompt if env var is set
PASSWORD = getpass.getpass()
tn.read_until(b'Password: ', timeout=2)
tn.write(PASSWORD.encode('ascii') + b'\n')
# Example: Send a command and read output
tn.write(b'ls -l\n')
output = tn.read_until(b'$ ', timeout=5) # Assuming a common shell prompt
print(output.decode('ascii'))
tn.write(b'exit\n')
print(tn.read_all().decode('ascii'))
except Exception as e:
print(f"An error occurred: {e}")
finally:
if 'tn' in locals() and tn:
tn.close()
Debug
Known issues
breakingThe `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.fixInstall `standard-telnetlib` via `pip install standard-telnetlib` to make the module available on newer Python versions.
affects: Python 3.13+
breakingThe 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.fixFor 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.
affects: All versions (inherent to Telnet protocol)
gotchaAll 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.fixEnsure 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. affects: All versions
gotchaThe `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.fixAlways convert the port number to an integer before passing it to the `Telnet` constructor, e.g., `telnetlib.Telnet(host, int(port))`.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'telnetlib'
The `telnetlib` module was removed from the Python standard library in Python 3.13.
fixInstall the `standard-telnetlib` package: `pip install standard-telnetlib`.
TypeError: a bytes-like object is required, not 'str'
Attempting to write a Python string object directly to the Telnet connection, which expects bytes.
fixEncode the string to bytes before writing: `tn.write('my_command'.encode('ascii') + b'\n')`. socket.gaierror: [Errno 10109] getaddrinfo failed
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.
fixEnsure the `port` is an integer: `telnetlib.Telnet(HOST, int(PORT))`. Also, verify the `HOST` value is a correct IP address or resolvable hostname.
EOFError: Telnet connection closed
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.
fixCheck 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.
Upgrade
Version history
3.13.0latest on PyPI · released Oct 30, 2024
Audit
Dependencies
No dependency data recorded yet.