In this guide to writing a network client application, I explain how to write a fun and easy python program for beginners. The result surprises but in a nice way.
A Brief TELNET Background
TCP/IP is a technology that glues the internet together, with many applications using the suite of protocols throughout the Internet and the World Wide Web.
Common uses of TCP/IP are found in browsing, chatrooms, and streaming. Enterprises use the suite to monitor and establish security in software. Every day individuals use it to stream online content and visit websites. The two types of application protocols are standard and non-standard.
TELNET is a standard application protocol for remote login within the TCP/IP suite. This protocol defines how data is sent to a remote device and the format of messages sent back by specifying the character data encoding for transmission. To most users, the details of how this protocol works are not necessary for the task (Comer, Stevens).
First, you will need an IDE or Integrated Development Environment to write python code. Admittedly, there are many to choose from which can seem daunting. However, I use Anaconda’s Jupyter Notebook in this guide. You can find the download here.
The Easy Method
After setting up your environment, launch Jupyter Notebook from Anaconda’s Navigator. Thankfully, python comes with the standard library called telnetlib. All you do is import the library by typing import telnetlib in the empty cell.
Next, specify the server’s hostname by typing the IP address or domain name. On a new line, specify the port. TELNET uses TCP port 23.
HOST = '22.214.171.124' PORT = 23
Then, establish a TELNET connection that listens to a server by typing the following.
tn = telnetlib.Telnet(HOST)
Finally, print the results that stream from the IP address specified above with the following entry.
There are many read* functions within the telnetlib library that I have not discussed. For further information on telnetlib, check out the official python website. There’s even another interesting TELNET implementation at the bottom of the documentation.
Test Your Creation
To view the result of your hard effort, select ‘Run’ from the menu or press ‘Shift + Enter’ on your keyboard for the selected cell. Definitely experiment further by adjusting the timeout value and improve upon this code in your own way.
A Challenging Method
import socket import time HOST = '126.96.36.199' # The server's hostname or IP address PORT = 23 # The port used by the server s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT)) full_msg = '' # Multiply by real number to view more/less data (e.g. 60*5) means 5 minutes end_time = time.time() + 60 while True: msg = s.recv(1024) if time.time() > end_time: break if len(msg) <= 0: break full_msg += msg.decode('ascii') print(full_msg) s.close()
Run this code in a new cell and wait the allotted amount of time to see the data that arrives through the telnet connection.
I hope you enjoyed this simple guide to writing a network client application in python to communicate with a server over the TELNET protocol. Happy learning 😀
Be sure to check out my post on TCP Sockets for Network Programming for more detail into socket functions.