def getMessage(self): message = "" message += enc.encrypt(self.command) message += "\n" message += enc.encrypt(str(self.data.count("\n") + 1)) message += "\n" for line in self.data.split("\n"): message += enc.encrypt(line) message += "\n" return message
def getMessage(self): message = "" message += enc.encrypt(self.command) message += "\n" message += enc.encrypt(str(self.data.count("\n")+1)) message += "\n" for line in self.data.split("\n"): message += enc.encrypt(line) message += "\n" return message
def accept_incoming_connections(self): # Sets up handling for incoming clients while True: client, client_address = self.server.accept() print("%s:%s has connected." % client_address) client.send(Encrypter.encrypt(self.key, b"You have entered the DarkRoom. Enter your alias.")) self.addresses[client] = client_address Thread(target=self.handle_client, args=(client,)).start()
def handle_client(self, client): # Takes client socket as argument # Handles a single client connection name = client.recv(self.BUFFER_SIZE) name = Encrypter.decrypt(self.key, name) welcome = b'Welcome, %s. If you want to quit, type !quit to exit.' % name client.send(Encrypter.encrypt(self.key, welcome)) msg = b"%s has joined the DarkRoom." % name self.broadcast(Encrypter.encrypt(self.key, msg)) self.clients[client] = name while True: msg = client.recv(self.BUFFER_SIZE) msg = Encrypter.decrypt(self.key, msg) if msg != b"!quit": self.broadcast(Encrypter.encrypt(self.key, bytes(name + b": " + msg))) else: client.send(Encrypter.encrypt(self.key, b"!quit")) print("%s:%s has disconnected." % addresses[client]) client.close() del clients[client] self.broadcast(Encrypter.encrypt(self.key, b"%s has left the DarkRoom." % name)) break