Introduction
The telnetlib is a module in the standard Python library. It provides the telnetlib.Telnet class for implementing the Telnet function. See RFC854 for details about the protocol: https://datatracker.ietf.org/doc/html/rfc854.html.
Different methods in the telnetlib.Telnet class are called to implement different functions.
from telnetlib import Telnet # Import the Telnet class of the telnetlib module.
tn = Telnet(host=None, port=0[, timeout]) # Create a Telnet connection to a specified server.
tn.read_all() # Invoke the read_all() method.

For more information, see https://docs.python.org/3/library/telnetlib.html.
Configuration example

The implementation process is as follows:
1. Configure the Telnet server
[Huawei] user-interface vty 0 4
[Huawei-ui-vty0-4] authentication-mode password
[Huawei-ui-vty0-4] set authentication password simple Huawei@123
[Huawei-ui-vty0-4] protocol inbound telnet
[Huawei-ui-vty0-4] user privilege level 15
[Huawei-ui-vty0-4] quit
[Huawei] telnet server enable
2. Manually verify and view the Telnet login procedure as a reference for code implementation.
Using the Windows OS as an example:
C:\>telnet 192.168.10.10
Login authentication
Password:
Info: The max number of VTY users is 5, and the number of current VTY users on line is 1.
The current login time is 2021-01-01 04:00.
%
<Huawei>
3. Compile and run Python code.
import telnetlib # Imports the module.
host ='192.168.10.10' # Sets the IP address for a host
password = 'Huawei@123' # Sets the password for logging in to the device
tn = telnetlib.Telnet(host) # Logs in to the host through Telnet.
tn.read_until(b'Password:') # Prints data until 'Password: ' is displayed
tn.write(password.encode('ascii') + b'\n') # Sets an ASCII password and starts a new line
print (tn.read_until(b'<Huawei>').decode('ascii)) # Prints data until <Huawei> is displayed.
tn.close() # Closes the Telnet connection.
In Python, the encode() and decode() functions are used to encode and decode strings in a specified format, respectively. In this example, password.encode('ascii') is to convert the string HuaweiQ@123 into the ASCll format. The encoding format complies with the official requirements of the telnetlib module.
Add a string b, b'str', indicating that the string is a bytes object. In this example, b'Password': indicates that the string Password:' is converted into a string of the bytes type. The encoding format complies with the official requirements of the telnetlib module.
For more information about Python objects, see
https://docs.python.org/3/reference/datamodel.html#objects-values-and-types.
4. Verify the result.
# Run Python code in the compiler.
Info: The max number of VTY users is 5, and the number of current VTY users online is 1.
The current login time is 2021-01-01 04:00.
%
<Huawei>

