Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket, os, json, random
- from time import sleep
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- s.bind(("127.0.0.1", random.randint(1111,9999)))
- address = (input("Enter server IP: "),int(input("Enter server port: ")))
- class Comms:
- Usr_Id = ""
- Type = ""
- data = ""
- timeStamp = None
- def __init__(self, Usr_Id, Type, Data, TimeStamp):
- self.Usr_Id = ""
- self.Type = ""
- self.data = ""
- self.timeStamp = None
- def sendMessage(message):
- dictionary = message.__dict__
- message = json.dumps(dictionary)
- s.sendall(message.encode("utf-8"))
- updateList = []
- def update():
- global updateList
- message = Comms(alias,"update",None,None)
- sendMessage(message)
- while True:
- data = s.recv(4096)
- if not data:
- pass
- else:
- data = data.decode("utf-8")
- jsonOut = json.loads(data)
- if jsonOut["Type"] == "endReq":
- break
- else:
- message = "{}->{}".format(jsonOut["Usr_Id"],jsonOut["data"])
- updateList.append(message)
- if updateList == []:
- print("No messages since last check.")
- else:
- for i in range(len(updateList)):
- print(updateList[i])
- updateList = []
- while True:
- alias = input("Enter desired name: ")
- s.connect(address)
- usrAuth = Comms(None,"usrAuth",alias,None)
- sendMessage(usrAuth)
- while True:
- data = s.recv(4096)
- if not data:
- pass
- else:
- break
- s.shutdown(2)
- data = data.decode("utf-8")
- jsonOut = json.loads(data)
- if jsonOut["data"] == "True":
- break
- else:
- print("Sorry, that name is taken. Try a different name.")
- sleep(3)
- os.system("cls")
- print("Connected! You can now send and recieve messages!")
- while True:
- command = input("->")
- command = list(command)
- if command[0] == "/":
- update()
- elif "".join(command) != "q":
- command = "".join(command)
- message = Comms(alias,"msg",command,None)
- sendMessage(message)
- else:
- s.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement