#!/usr/bin/env python3 # telnet program example import socket, select, sys #main function if __name__ == "__main__": if(len(sys.argv) < 2) : print('Usage : {} hostname'.format(sys.argv[0])) sys.exit() host = sys.argv[1] port = 1234 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. Start sending messages') print("", end=" ", flush=True) 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 data : print(data.decode("UTF-8")) print("", end=" ", flush=True) else : print("\nDisconnected from chat server") sys.exit() #user entered a message else : msg = sys.stdin.readline() s.send(msg.encode("UTF-8")) print("", end=" ", flush=True)