Last active
April 8, 2020 14:27
-
-
Save ertugrulturan/0101b08bbfa11d01d82517194c9c58b6 to your computer and use it in GitHub Desktop.
Python Telnet Chat Client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket, select, string, sys | |
def prompt() : | |
sys.stdout.write('<Sen> ') | |
sys.stdout.flush() | |
#main function | |
if __name__ == "__main__": | |
if(len(sys.argv) < 3) : | |
print 'kullan : python telnet.py ip 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. Start sending messages' | |
prompt() | |
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 '\nDisconnected from chat server' | |
sys.exit() | |
else : | |
#print data | |
sys.stdout.write(data) | |
prompt() | |
#user entered a message | |
else : | |
msg = sys.stdin.readline() | |
s.send(msg) | |
prompt() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment