# Python Telnetlib Module

The `telnetlib` module provides a Telnet client class that implements the Telnet protocol.

**Note: The `telnetlib` module is deprecated as of Python 3.11 and was removed in Python 3.13. It is recommended to use external libraries like `telnetlib3` or `asyncio` for network communication.**

## Importing the Module

```python
import telnetlib
```

## Basic Usage

To connect to a Telnet server:

```python
import telnetlib

HOST = "localhost"
user = "user"
password = "password"

tn = telnetlib.Telnet(HOST)

tn.read_until(b"login: ")
tn.write(user.encode('ascii') + b"\n")

tn.read_until(b"Password: ")
tn.write(password.encode('ascii') + b"\n")

tn.write(b"ls\n")
tn.write(b"exit\n")

print(tn.read_all().decode('ascii'))
```

## Debugging

You can set a debug level to see the communication details.

```python
tn.set_debuglevel(1)
```

## Methods

*   `read_until(expected, timeout=None)`: Read until a given byte string, `expected`, is encountered or until `timeout` seconds have passed.
*   `read_all()`: Read all data until EOF; block until connection closed.
*   `write(buffer)`: Write a byte string to the socket.
*   `interact()`: Interaction function, emulates a very dumb Telnet client.

[[programming/python/python]]