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



How to create telnet client with asyncio in Python

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.  

Similar Reads

Steps to create a telnet client

1. Define an asynchronous telnet client function...

Telnet server

...