count_connections += 1 print("CONNECTION " + str(count_connections) + ".Client IP, PORT: " + str(client_ip_port)) # -- Server stopped manually except KeyboardInterrupt: print("Server stopped by the user") # -- Close the listenning socket ls.close() # -- Exit! exit() msg_raw = cs.recv(2048) msg = msg_raw.decode() formatted_message = server_utils.format_command(msg) print(formatted_message) formatted_message = formatted_message.split(" ") if len(formatted_message) == 1: command = formatted_message[0] else: command = formatted_message[0] argument = formatted_message[1] if formatted_message == "PING": server_utils.ping(cs) elif command == "GET": server_utils.get(cs, l_seqs, argument)
# -- Server stopped manually except KeyboardInterrupt: print("Server stopped by the user") ls.close() exit() # -- Read the message from the client # -- The received message is in raw bytes msg_raw = cs.recv(2048) print(msg_raw) # -- We decode it for converting it # -- into a human-redeable string msg = msg_raw.decode() formatted_msg = server_utils.format_command(msg) print(formatted_msg) # if len(formatted_msg) == 1: #command = formatted_msg[0] #else:: #command = formatted_msg[0] #argument = formatted_msg[1] #if formatted_msg == "'PING'": #server_utils.ping() # -- Send a response message to the client #response = 'ok' # cs.send(str(response).encode()) #else: # response = 'NO available command'
# -- Exit! exit() # -- Execute this part if there are no errors print("A client has connected to the server!") # -- Read the message from the client # -- The received message is in raw bytes msg_raw = cs.recv(2048) # -- We decode it for converting it # -- into a human-redeable string msg = msg_raw.decode() formatted_message = server_utils.format_command(msg).replace('"', '') formatted_message = formatted_message.split(" ") # print(formatted_message) if len(formatted_message) == 1: command = formatted_message[0] else: command = formatted_message[0] argument = formatted_message[1] if command == "PING": server_utils.ping(cs) elif command == "GET": print(msg, ":", server_utils.get(cs, list_sequences, argument)) elif command == "INFO":
try: (cs, client_ip_port) = ls.accept() client_address_list.append(client_ip_port) count_connections += 1 print("CONNECTION " + str(count_connections) + ". Client IP, PORT: " + str(client_ip_port)) except KeyboardInterrupt: print("Server stopped by the user") ls.close() exit() msg_raw = cs.recv(2048) msg = msg_raw.decode() formatted_message = server_utils.format_command(msg) # To eliminate \n \r formatted_message = formatted_message.split( " ") # For creating a list of the division. if len(formatted_message) == 1: command = formatted_message[0] else: command = formatted_message[0] argument = formatted_message[1] if command == "PING": server_utils.ping(cs) cs.close() elif command == "GET": server_utils.get(cs, list_sequences, argument) cs.close()
ls = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ls.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) ls.bind((IP, PORT)) ls.listen() print("SEQ Server up!") while True: print("Awaiting client...") try: (cs, client_ip_port) = ls.accept() except KeyboardInterrupt: print("Server stopped by the user") ls.close() exit() else: # print("A client has connected to the server!") msg = utils.format_command(cs.recv(2048).decode()) if msg == "PING": res = "OK!" # print(termcolor.colored("PING command!", "green")) utils.ping() elif "GET" in msg: res = list_seq[int(msg[-1])] utils.print_colored("GET", "green") elif "INFO" in msg: utils.print_colored("INFO", "green") res = utils.info(msg.replace("INFO ", "")) elif "COMP" in msg: utils.print_colored("COMP", "green") res = utils.comp(msg.replace("COMP ", "")) elif "REV" in msg: utils.print_colored("REV", "green")