def disconnect_client(self): """ Shuts down the client-server conncetion and the threads are dissolved """ self.server_socket.shutdown(socket.SHUT_RDWR) self.server_socket.close() self.end_threads() sysprint("Disconnected!")
def get_active_members(self): """ Recieves the list of active members in current from the server and prints them """ self.send('.') active_members = json.loads(self.get()) sysprint("Active members in {}".format(self.group_name)) for num, member in enumerate(active_members): sysprint("{}. {}".format(num + 1, member))
def send_info(self): """ Sends the user information to the server in order to be logged in """ self.send(self.user_name) command = self.get() # Ununsed command self.send(self.group_name) command = self.get() if command == CODE['ready']: sysprint("You have joined the group {}!".format(self.group_name)) self.active = True elif command == CODE['error']: sysprint("[ERROR]")
def main(self): """ Accepts incoming connections """ self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.server_socket.bind(self.ADDRESS) self.server_socket.listen() sysprint("Server is now Listening") while True: client_socket, address = self.server_socket.accept() user = self.create_user(client_socket, address) self.serve(user)
def get_file(self): self.send(CODE['sendFileName']) message = self.get() file_name, file_size = message.split(SEPERATOR) self.send(CODE['sendFile']) file_name = os.path.basename(file_name) file_size = int(file_size) with open(file_name, "wb") as file: gotten = 0 while gotten < file_size: bytes_read = self.server_socket.recv(BUFFER) file.write(bytes_read) gotten += len(bytes_read) sysprint("Recieved {}!".format(file_name))
def send_file(self): """Sends a file to the server. """ file_name = input(colored("Enter file name: ", SYS_COLOR)) try: open(file_name, 'rb') except Exception as e: sysprint("[ERROR] : {}".format(e)) self.send(CODE['error']) return file_size = os.path.getsize(file_name) self.send("{}{}{}".format(file_name, SEPERATOR, file_size)) command = self.get() if command != CODE['ready']: sysprint("Error at server side") return with open(file_name, 'rb') as file: while True: bytes_read = file.read(BUFFER) if not bytes_read: break self.server_socket.send(bytes_read) self.send(CODE['all_sent']) sysprint("Sent to server!")
def send_file(self, user: User, file_name: str, file_size: str): for users in self.groups[user.group_name].active_members: if users == user: continue self.send(users, CODE['fileTransferC']) with self.condition: self.condition.wait() name_size = SEPERATOR.join([file_name, file_size]) self.send(users, name_size) with self.condition: self.condition.wait() with open(file_name, 'rb') as file: while True: bytes_read = file.read(BUFFER) if not bytes_read: break users.client_socket.send(bytes_read) sysprint("Sent to user {}".format(users.user_name)) os.remove(file_name)
def print_menu(self): """ Prints the command menu for the user """ sysprint("You can do these operations by typing such commands") sysprint("/1 : Disconnect") sysprint("/2 : Display Active Users") sysprint("/3 : Send a file") sysprint("/4 : Print menu again") sysprint("Type anything else to send a message")