Advertisement
Guest User

DISCO Chat Client

a guest
May 28th, 2016
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.96 KB | None | 0 0
  1. #!/usr/bin/python2
  2.  
  3. # sockets are the interface to network
  4. # connections for programmers
  5. import socket
  6.  
  7. # the regular expressions are needed
  8. # to parse the protocol; see FGDP or
  9. # https://docs.python.org/2/howto/regex.html
  10. import re
  11.  
  12. # https://docs.python.org/2/library/threading.html#threading.Timer
  13. from threading import Timer
  14.  
  15. # some global variables for configuration
  16. username = "123456"
  17. password = "name@provider.com"
  18. chathost = "131.246.19.102"
  19. chatport = 1337
  20.  
  21. # setup connection to the chat server
  22. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  23. s.setblocking(True)
  24. s.connect((chathost, chatport))
  25.  
  26. last = None
  27. def resend():
  28.     global s, last
  29.     s.send(last+"\r\n")
  30.  
  31. def send(msg):
  32.     global s, last
  33.     s.send(msg+"\r\n")
  34.     last = msg
  35.  
  36. # line by line
  37. for m in s.makefile():
  38.    
  39.     msg = re.match("^([0-9]+)(#(.+))?\r$", m)
  40.    
  41.     if msg is None:
  42.         print "Warning: server sent message with unknown format:\n\t'%s'" % m
  43.         continue # skip this message
  44.    
  45.     cmd, _, args = msg.groups()
  46.    
  47.     # server message
  48.     if cmd == "0":
  49.         print "Server: %s" % args
  50.        
  51.     # servers requests username
  52.     elif cmd == "1":
  53.         print "Server: %s" % args
  54.         send("1#%s" % username)
  55.         print "Client: My name is '%s'" % username
  56.  
  57.     # server requests password
  58.     elif cmd == "2":
  59.         print "Server: %s" % args
  60.         send("2#%s" % password)
  61.         print "Client: Password is '%s'" % password
  62.    
  63.     # logged in successfully
  64.     elif cmd == "3":
  65.         print "Server: %s" % args
  66.         send("6") # request list of online users
  67.         print "Client: Who's online?"
  68.    
  69.     # another user logged in
  70.     elif cmd == "4":
  71.         print "Server: User '%s' joined the chat." % args
  72.    
  73.     # another user logged out
  74.     elif cmd == "5":
  75.         print "Server: User '%s' left the chat." % args
  76.    
  77.     # list of online users
  78.     elif cmd == "6":
  79.         usrs = args.split("#")
  80.         print "Server: Online users are "+", ".join(usrs)
  81.         send("9")
  82.         print "Client: Which exercises did I pass so far?"
  83.    
  84.     # message from another user
  85.     elif cmd == "7":
  86.         user, text = re.match("^(.+?)#(.+)$", args).groups()
  87.         print "%s: %s" % (user, text)
  88.    
  89.     # list of all finished assignments
  90.     elif cmd == "9":
  91.         if (args == None):
  92.             print "Server: You've not completed any task yet? Come ooooonn...."
  93.             continue
  94.         exs = args.split("#")
  95.         print "Server: You've completed exercises "+", ".join(exs)
  96.         send("12") # start the game
  97.    
  98.     # received challenge response
  99.     elif cmd == "11":
  100.         print "Server: %s" % args
  101.        
  102.     # count up
  103.     elif cmd == "12":
  104.         print "Server: The counter is at %s." % args
  105.         send("12#"+args)
  106.    
  107.     # unknown message type
  108.     else:
  109.         print "Warning: Received unimplemented message of type %s." % cmd
  110.        
  111. print "Exiting..."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement