Advertisement
Guest User

Untitled

a guest
May 19th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.23 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. # some global variables for configuration
  13. username = "123456"
  14. password = "random@rhrk.uni-kl.de"
  15. chathost = "131.246.19.102"
  16. chatport = 1337
  17.  
  18. # setup connection to the chat server
  19. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  20. s.setblocking(True)
  21. s.connect((chathost, chatport))
  22.  
  23. print "Note: Press CTRL+C to exit client.\n"
  24.  
  25. def primfaktoren(n):
  26.     faktoren = []
  27.     z = n
  28.     while z > 1:
  29.         # bestimme den kleinsten Primfaktor p von z
  30.         i = 2
  31.         gefunden = False
  32.         while i*i <= n and not gefunden:
  33.             if z % i == 0:
  34.                 gefunden = True
  35.                 p = i
  36.             else:
  37.                 i = i + 1
  38.         if not gefunden:
  39.             p = z
  40.         faktoren = faktoren + [p]
  41.         z = z // p
  42.     return faktoren
  43.  
  44. # line by line
  45. for m in s.makefile():
  46.     msg = re.match("^([0-9]+)(#(.+))?\r\n$", m)
  47.  
  48.     if msg is None:
  49.         print "Warning: server sent message with unknown format:\n\t'%s'" % m
  50.         continue # skip this message
  51.  
  52.     cmd, _, args = msg.groups()
  53.  
  54.     # server message
  55.     if cmd == "0":
  56.         print "Server: %s" % args
  57.  
  58.     # servers requests username
  59.     elif cmd == "1":
  60.         print "Server: %s" % args
  61.         s.send("1#%s\r\n" % username)
  62.         print "Client: My name is '%s'" % username
  63.  
  64.     # server requests password
  65.     elif cmd == "2":
  66.         print "Server: %s" % args
  67.         s.send("2#%s\r\n" % password)
  68.         print "Client: Password is '%s'" % password
  69.  
  70.     # logged in successfully
  71.     elif cmd == "3":
  72.         print "Server: %s" % args
  73.         s.send("6\r\n") # request list of online users
  74.         print "Client: Who's online?"
  75.  
  76.     # another user logged in
  77.     elif cmd == "4":
  78.         print "Server: User '%s' joined the chat." % args
  79.  
  80.     # another user logged out
  81.     elif cmd == "5":
  82.         print "Server: User '%s' left the chat." % args
  83.  
  84.     # list of online users
  85.     elif cmd == "6":
  86.         usrs = args.split("#")
  87.         print "Server: Online users are "+", ".join(usrs)
  88.         s.send("9\r\n")
  89.         print "Client: Which exercises did I pass so far?"
  90.  
  91.     # message from another user
  92.     elif cmd == "7":
  93.         user, text = re.match("^(.+?)#(.+)$", args).groups()
  94.         print "%s: %s" % (user, text)
  95.  
  96.     # list of all finished assignments
  97.     elif cmd == "9":
  98.         if (args == None):
  99.             print "Server: You've not completed any task yet? Come ooooonn...."
  100.             continue
  101.         exs = args.split("#")
  102.         print "Server: You've completed exercises "+", ".join(exs)
  103.         s.send("10\r\n")
  104.  
  105.     elif cmd == "10":
  106.         print "Server: " + args
  107.         send = "11"
  108.         for p in primfaktoren(int(args)):
  109.             send += "#" + str(p)
  110.         send += "\r\n"
  111.         s.send(send)
  112.         print "Client: " + send
  113.  
  114.     elif cmd == "11":
  115.         print "Server: " + args
  116.  
  117.     # unknown message type
  118.     else:
  119.         print "Warning: Received unimplemented message of type %s." % cmd
  120.  
  121. print "Exiting..."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement