Skip to content

Instantly share code, notes, and snippets.

@AjayKrP
Last active October 17, 2017 09:15
Show Gist options
  • Save AjayKrP/c179d2207379a159460e0cbee416a120 to your computer and use it in GitHub Desktop.
Save AjayKrP/c179d2207379a159460e0cbee416a120 to your computer and use it in GitHub Desktop.
import socket, select, string, sys
def prompt() :
sys.stdout.write('<You> ')
sys.stdout.flush()
#main function
if __name__ == "__main__":
if(len(sys.argv) < 3) :
print 'Usage : python 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)
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]
read_sockets, write_sockets, error_sockets = select.select(socket_list , [], [])
for sock in read_sockets:
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()
else :
msg = sys.stdin.readline()
s.send(msg)
prompt()
#[ajay@localhost multi-user-chat]$ python client.py 127.0.0.1 5000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment