How to Code a simple Telnet Client with Sockets in Python

By | August 1, 2020

The telnet client is a simple commandline utility that is used to connect to socket servers and exchange text messages.

Here is an example of how to use telnet to connect to google.com and fetch the homepage.

$ telnet google.com 80

The above command will connect to google.com on port 80.

$ telnet google.com 80
Trying 74.125.236.69...
Connected to google.com.
Escape character is '^]'.

Now that it is connected, the telnet command can take user input and send to the server, and whatever the server replies with, will be displayed on the terminal. For example send the http GET command and hit enter twice.

GET / HTTP/1.1

Sending the above will generate some response from the server. Now we are going to make a similar telnet program in python. The program is short and simple.

Code

To implement a program that takes user input and fetches results from the remote server at the same, requires somekind of parallel processing. Now the obvious solution to this is to use threads.

One thread to keep receiving message from server and another to keep taking in user input. But there is another way to do this apart from threads.

And that is select function. Select function allows to monitor multiple sockets/streams for readability and will generate an even if any of the sockets is ready.

Lets take a look at the full code. Its less than 50 lines including comments.

# telnet program example
import socket, select, string, sys

#main function
if __name__ == "__main__":
	
	if(len(sys.argv) < 3) :
		print 'Usage : python telnet.py hostname port'
		sys.exit()
	
	host = sys.argv[1]
	port = int(sys.argv[2])
	
	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	s.settimeout(2)
	
	# connect to remote host
	try :
		s.connect((host, port))
	except :
		print 'Unable to connect'
		sys.exit()
	
	print 'Connected to remote host'
	
	while 1:
		socket_list = [sys.stdin, s]
		
		# Get the list sockets which are readable
		read_sockets, write_sockets, error_sockets = select.select(socket_list , [], [])
		
		for sock in read_sockets:
			#incoming message from remote server
			if sock == s:
				data = sock.recv(4096)
				if not data :
					print 'Connection closed'
					sys.exit()
				else :
					#print data
					sys.stdout.write(data)
			
			#user entered a message
			else :
				msg = sys.stdin.readline()
				s.send(msg)

Small program! Just run it in a terminal like this

$ python telnet.py google.com 80
Connected to remote host

Once connected it shows the connected message. Once the message pops up, its time to type in some message to send to the remote server. Type the same GET message and send by hitting enter twice. Some response should be generated.

The above program does the task of listening for message from remote server and listening for user input at the same time, and without threads.

socket_list = [sys.stdin, s]
		
# Get the list sockets which are readable
read_sockets, write_sockets, error_sockets = select.select(socket_list , [], [])

The socket list contains 2 sockets. First is the sys.stdin which is stream for standard input or the user input at the command line. The other one is the socket that is connected to remote server. The select function keeps listening on both of them. It is a blocking function and returns if either of the 2 things happens

1. Server sends a message
2. User hits enter after typing in a message

If the server socket is ready to be read, then just call recv function on it. If the user input is ready to be read then call sys.stdin.readline() function get the user message. Thats all about it.

The telnet client shown above is a minimal one. The actual telnet client has lots of other features which you can try to implement.

The above telnet client can be used as a terminal chat client as well with little modifications. Just have to write a chat server. Will come up with a post on that soon.

Note

The above shown program will work only on linux and not on windows. The program uses the select function read the command line input (stdin).

On windows the select function cannot read file descriptors. It can only read sockets created inside winsock. The python documentation on select function mentions this

File objects on Windows are not acceptable, but sockets are. On Windows, the underlying select() function is provided by the WinSock library, and does not handle file descriptors that don’t originate from WinSock.
About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

12 Comments

How to Code a simple Telnet Client with Sockets in Python
  1. ali

    hello and tnx.
    for me it doesn’t work. and i have error in this line:
    read_sockets, write_sockets, error_sockets = select.select(socket_list, [], []).

  2. murs

    Very useful article! I am a big fan of hello world type of code and this was perfect for me to get started. Specifically, I am trying to telnet from an Amazon Lambda function. With a slight modification of your code I was able to get it working.

    Thank you very much!

  3. Gabriel Schubert

    Hello Silver Moon.

    I’m using your code to make communication with the controller of my Mitsubishi robot, sending commands to him and geting the readout. Everything works fine. I’ve just changed to defined commands, not keyboard entry.

    You saved my life.

    Thank you!

    Best Regards,
    Gabriel.

  4. Jason

    I got a problem.

    Traceback (most recent call last):

    File “telnet.py”, line 34, in

    read_sockets,write_sockets,error_sockets = select.select(socket_list,[],[])

    select.error: (10038, ‘An operation was attempted on something that is not a soc

    ket’)

    Anybody knows about it?
    I know the sys.stdin is not a socket. So how do you run it?

    1. Rudolf

      Hello Silver Moon,
      Tank you very much for the interesting program.
      Unfortunately is does not work in Python3:
      read_sockets, write_sockets, error_sockets = select.select(socket_list , [], [])
      io.UnsupportedOperation: fileno

      Is there a solution for it?

      Best regards, Rudolf

Leave a Reply

Your email address will not be published. Required fields are marked *