How to create telnet client with asyncio in Python
Last Updated :
24 Apr, 2025
Telnet is a client/server application protocol that uses TCP/IP for connection. Telnet protocol enables a user to log onto and use a remote computer as though they were connected directly to it within the local network. The system that is being used by the user for the connection is the client and the remote computer being connected is the server. The commands entered on the terminal in a Telnet client are executed on the server (the remote computer) and the output of the command is directed to the client computer's screen. Telnet is insecure as it uses plain text communication. The secure implementation of telnet that uses cryptography to encrypt data being transferred is SSH (Secure Shell). The telnet protocol is I/O bound, the I/O is very slow for e.g, the establishment of a connection with the server may take a long time due to slow speed on the server side, this results in a lot of idle CPU time since many such connections need to be created to the server, the asyncio library is the most suitable for such a task.
Asyncio (Asynchronous Input/Output) is a python library for concurrent programming. Asynchronous is a style of programming in which two processes are being executed at the same time (not simultaneously). There is only one CPU core involved in computation (single thread), due to which the tasks take turns for execution and processing. Task 1 may start first and then mid-process the CPU starts execution of Task 2, the CPU may choose to switch between the tasks to optimize the usage of resources and minimize idle time.
Steps to create a telnet client
1. Define an asynchronous telnet client function
Using the asyncio.open_connection() coroutine with parameters host, port open a connection to the server, this function returns a reader and a writer that are instances of the class Streamreader and Streamwriter. These objects are used to read from the server and write to the server.
Write the username and password through the Streamwriter object to authenticate.
The statement awaits the reader.readline() returns a line that is received from the server, and yields the control while waiting for a response from the server, yielding control means informing the event loop that while the coroutine is waiting for a response go on and use the processing power for other computations. Print the response that is received from the server.
Once a connection is established successfully the writer object of the Streamwriter class can be used to send text commands that the server can implement locally and the results can be read via the object of the Streamreader class.
Close the connection.
2. Define an asynchronous main
Within the main function call the telnet_client() coroutine with parameters hostname, a port that the server is listening on, username, and password. The await call yields the control while the telnet_client() coroutine is waiting for a connection.
3. Asynchronous run the main function
The top-level entry point main() coroutine (since it is defined with async def) does not implement itself but rather needs to be run with the command.
Python3
import asyncio
async def telnet_client(host: str, port: int, username: str, password: str) -> None:
reader, writer = await asyncio.open_connection(host, port)
print(f"connected to ({host}, {port})")
# Login to the server, if the server requires authentication
""" await writer.write(f"{username}\n".encode())
await writer.write(f"{password}\n".encode()) """
while True:
command = input("\nEnter a command: ")
if not command:
print("[No command] ...closing connection ")
break
writer.write(f"{command}\n".encode())
data = await reader.read(100000)
print(data.decode().strip())
# there is a telnet server (telnet_server.py) listening on localhost port=23
asyncio.run(telnet_client("127.0.0.1", 2000, "username", "password"))
Telnet server
To test the client you must have a server to establish a connection to, following is a local server in python listening on port 2000. The setup is very similar to the client with the handle_client function working as the asynchronous listener. The run_command function spawns a process with command com, the stdin, stdout, stderr streams of the shell are captured and the data is returned to the client using the StreamWriter.
Python3
import asyncio
import subprocess
def run_command(com: str) -> None:
try:
pro = subprocess.run(com.split(), capture_output=True, text=True)
if pro.stdout:
return f"out----------------\n{pro.stdout}"
elif pro.stderr:
return f"err----------------\n {pro.stderr}"
else:
return f"[executed]"
except Exception as ex:
print("exception occurred", ex)
return f" [subprocess broke]"
async def handle_client(reader, writer):
print(f"Connected to {writer.get_extra_info('peername')}")
while True:
data = await reader.read(100000)
message = data.decode().strip()
if not message:
break
print(f"Received message: {message}")
res = run_command(message)
writer.write(res.encode())
print("Closing connection")
writer.close()
async def start_server():
server = await asyncio.start_server(handle_client, "127.0.0.1", 2000)
print("Server started")
await server.serve_forever()
asyncio.run(start_server())
Now run the following command in the terminal:
python telnet_server.py
python telnet_client.py
Output:
server started
client connected
commands executed
Similar Reads
How to create a meeting with zoom API in Python?
In this article, we will discuss how to create a zoom meeting with the help of zoom API using Python. To integrate zoom API, we need to create it. Follow the given steps for the same: To use the zoom API first visit https://marketplace.zoom.us/ and either sign up or sign in with your zoom account.No
6 min read
Telnet with ipaddress and port in Python
Telnet is a protocol that allows you to connect to a remote computer and execute commands. Python provides a built-in module called "telnetlib" that enables us to use Telnet in our code. What is Telnet in Python?Telnet clients let you connect to other Telnet Servers. You cannot connect to your own s
3 min read
Create a ChatBot with OpenAI and Streamlit in Python
ChatGPT is an advanced chatbot built on the powerful GPT-3.5 language model developed by OpenAI.There are numerous Python Modules and today we will be discussing Streamlit and OpenAI Python API to create a chatbot in Python streamlit. The user can input his/her query to the chatbot and it will send
5 min read
How to Create a Non-Blocking Server in Java?
A Non-Blocking server means that it is able to have multiple requests in progress at the same time by the same process or thread because it uses Non-Blocking I/O. In the Non-Blocking approach - one thread can handle multiple queries at a time. A server using a Non-Blocking socket works in an asynchr
7 min read
How to Use ChatGPT API in Python?
ChatGPT and its inevitable applications. Day by Day everything around us seems to be getting automated by several AI models using different AI and Machine learning techniques and Chatbot with Python , there are numerous uses of Chat GPT and one of its useful applications we will be discussing today.
6 min read
How To Fix âConnectionerror : Max retries exceeded with URLâ In Python?
When the Python script fails to establish a connection with a web resource after a certain number of attempts, the error 'Connection error: Max retries exceeded with URL' occurs. It's often caused by issues like network problems, server unavailability, DNS errors, or request timeouts. In this articl
3 min read
How to Fix 'Waiting in Queue' Error in Python
In Python, handling and managing queues efficiently is crucial especially when dealing with concurrent or parallel tasks. Sometimes, developers encounter a 'Waiting in Queue' error indicating that a process or thread is stuck waiting for the resource or a task to complete. This error can stem from s
3 min read
Convert Text to Speech in Python using win32com.client
There are several APIs available to convert text to speech in python. One of such APIs available in the python library commonly known as win32com library. It provides a bunch of methods to get excited about and one of them is the Dispatch method of the library. Dispatch method when passed with the a
2 min read
Asynchronous HTTP Requests with Python
Performing multiple HTTP requests is needed while working with applications related to data preprocessing and web development. In the case of dealing with a large number of requests, using synchronous requests cannot be so efficient because each request must wait for the previous request to get comp
4 min read
How to Configure Socket.IO with Demo-Chat App in Node.js ?
For making a chat app, it is required that the server sends data to the client but the client should also respond back to the server. This can be done by making use of the Websockets. A WebSocket is a kind of communication pipe opened in two directions. Prerequisite: Node.js: It is an open source Ja
5 min read