Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2018
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.33 KB | None | 0 0
  1. import socket, os, json, random
  2.  
  3. from time import sleep
  4.  
  5. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  6.  
  7. s.bind(("127.0.0.1", random.randint(1111,9999)))
  8.  
  9. address = (input("Enter server IP: "),int(input("Enter server port: ")))
  10.  
  11. class Comms:
  12.  
  13.     Usr_Id = ""
  14.  
  15.     Type = ""
  16.  
  17.     data = ""
  18.  
  19.     timeStamp = None
  20.  
  21.     def __init__(self, Usr_Id, Type, Data, TimeStamp):
  22.  
  23.         self.Usr_Id = ""
  24.  
  25.         self.Type = ""
  26.  
  27.         self.data = ""
  28.  
  29.         self.timeStamp = None
  30.  
  31. def sendMessage(message):
  32.    
  33.     dictionary = message.__dict__
  34.  
  35.     message = json.dumps(dictionary)
  36.    
  37.     s.sendall(message.encode("utf-8"))
  38.  
  39. updateList = []
  40.  
  41. def update():
  42.  
  43.     global updateList
  44.  
  45.     message = Comms(alias,"update",None,None)
  46.  
  47.     sendMessage(message)
  48.  
  49.     while True:
  50.  
  51.         data = s.recv(4096)
  52.  
  53.         if not data:
  54.  
  55.             pass
  56.  
  57.         else:
  58.  
  59.             data = data.decode("utf-8")
  60.  
  61.             jsonOut = json.loads(data)
  62.  
  63.             if jsonOut["Type"] == "endReq":
  64.  
  65.                 break
  66.  
  67.             else:
  68.  
  69.                 message = "{}->{}".format(jsonOut["Usr_Id"],jsonOut["data"])
  70.  
  71.                 updateList.append(message)
  72.  
  73.     if updateList == []:
  74.  
  75.         print("No messages since last check.")
  76.  
  77.     else:
  78.    
  79.         for i in range(len(updateList)):
  80.  
  81.             print(updateList[i])
  82.  
  83.         updateList = []
  84.  
  85. while True:
  86.  
  87.     alias = input("Enter desired name: ")
  88.  
  89.     s.connect(address)
  90.  
  91.     usrAuth = Comms(None,"usrAuth",alias,None)
  92.  
  93.     sendMessage(usrAuth)
  94.  
  95.     while True:
  96.  
  97.         data = s.recv(4096)
  98.  
  99.         if not data:
  100.  
  101.             pass
  102.  
  103.         else:
  104.  
  105.             break
  106.  
  107.     s.shutdown(2)
  108.        
  109.     data = data.decode("utf-8")
  110.  
  111.     jsonOut = json.loads(data)
  112.  
  113.     if jsonOut["data"] == "True":
  114.  
  115.         break
  116.  
  117.     else:
  118.  
  119.         print("Sorry, that name is taken. Try a different name.")
  120.  
  121.         sleep(3)
  122.  
  123.         os.system("cls")
  124.  
  125. print("Connected! You can now send and recieve messages!")
  126.  
  127. while True:
  128.  
  129.     command = input("->")
  130.  
  131.     command = list(command)
  132.  
  133.     if command[0] == "/":
  134.  
  135.         update()
  136.  
  137.     elif "".join(command) != "q":
  138.  
  139.         command = "".join(command)
  140.  
  141.         message = Comms(alias,"msg",command,None)
  142.  
  143.         sendMessage(message)
  144.    
  145.     else:
  146.  
  147.         s.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement