def main(): # Initializes global users and questions dicionaries using load functions, will be used later global users global questions print("loading...") questions = load_questions() users = load_user_database() server_socket = setup_socket() print("Welcome to Trivia Server!") global clients_sockets while True: (ready_to_read, ready_to_write, in_error) = select.select([server_socket] + clients_sockets, [], []) for current_socket in ready_to_read: if current_socket is server_socket: (client_socket, client_adress) = current_socket.accept() print("new client joined ", client_adress, "(;") clients_sockets.append(client_socket) else: try: data = current_socket.recv(1024).decode() except: handle_logout_message(current_socket) break cmd, data1 = chatlib.parse_message(data) handle_client_message(current_socket, cmd, data1)
def recv_message_and_parse(conn): """ Receives a new message from given socket, then parses the message using chatlib. :param conn: (socket object) :return: cmd (str) and data (str) of the received message. If error occurred, will return None, None """ cmd, data = chatlib.parse_message(conn.recv(MAX_MSG_LENGTH).decode()) return cmd, data
def recv_message_and_parse(conn): """Recieves a new message from given socket, then parses the message using chatlib. Paramaters: conn (socket object) Returns: cmd (str) and data (str) of the received message. If error occured, will return None, None""" full_msg = conn.recv(1024).decode() #get data from the server cmd, data = chatlib.parse_message(full_msg) print('data recieved -> command: ', cmd, ' data: ', data) return cmd, data
def recv_message_and_parse(conn): """Recieves a new message from given socket, then parses the message using chatlib. Paramaters: conn (socket object) Returns: cmd (str) and data (str) of the received message. If error occured, will return None, None""" full_msg = str(conn.recv(1024).decode()) # get data from the server (cmd, data) = chatlib.parse_message(full_msg) print((conn.getpeername(), " sent:", data)) return cmd, data
def recv_message_and_parse(conn): """ Receives a new message from given socket, then parses the message using chatlib. :param: conn (socket object) :return: cmd (str) and data (str) of the received message. If error occurred, will return None, None """ msg = conn.recv(MAX_MSG_LENGTH).decode() print("[CLIENT] ", conn.getpeername(), msg) # Debug print cmd, data = chatlib.parse_message(msg) return cmd, data
def recv_message_and_parse(conn): """ Recieves a new message from given socket, then parses the message using chatlib. Paramaters: conn (socket object) Returns: cmd (str) and data (str) of the received message. If error occured, will return None, None """ full_msg = conn.recv(chatlib.MAX_MSG_LENGTH).decode() cmd, data = chatlib.parse_message(full_msg) return cmd, data
def check_parse(msg_str, expected_output): print("Input: ", msg_str, "\nExpected output: ", expected_output) try: output = chatlib.parse_message(msg_str) except Exception as e: output = "Exception raised: " + str(e) if output == expected_output: print(".....\t SUCCESS") else: print(".....\t FAILED, output: ", output)
def recv_message_and_parse(conn): """ Receives a new message from given socket, then parses the message using chatlib. Parameters: conn (socket object) Returns: cmd (str) and data (str) of the received message. If error occurred, will return None, None """ full_msg = conn.recv(1024).decode() cmd, data = chatlib.parse_message(full_msg) print("[CLIENT - " + socket.gethostbyaddr(str(conn.getpeername()[0]))[0] + "]", full_msg) # Debug print return cmd, data
def recv_message_and_parse(conn): global exit_msgs, CONNECTION_LOST conn_peername = str(conn.getpeername()) debug_print_msg = '' try: full_msg = conn.recv(chatlib.MAX_MSG_LENGTH).decode() debug_print_msg = "[" + conn_peername + "] SEND: " + full_msg if len(full_msg) == 0: return CONNECTION_LOST, conn_peername, debug_print_msg cmd, data = chatlib.parse_message(full_msg) if cmd in exit_msgs: data = conn_peername return cmd, data, debug_print_msg except ConnectionResetError: return CONNECTION_LOST, conn_peername, debug_print_msg
def recv_message_and_parse(): """ Recieves a new message from given socket, then parses the message using chatlib. Paramaters: conn (socket object) Returns: cmd (str) and data (str) of the received message. If error occured, will return None, None """ try: full_msg = top.conn.recv(chatlib.MAX_MSG_LENGTH).decode() cmd, data = chatlib.parse_message(full_msg) return cmd, data except ConnectionAbortedError: from gui import trivia trivia.vp_start_gui()
def recv_message_and_parse(conn): """ Receives a new message from given socket, then parses the message using chatlib. Parameters: conn (socket object) Returns: cmd (str) and data (str) of the received message. If error occurred, will return None, None """ try: full_msg = conn.recv(MAX_MSG_LENGTH).decode() except (ConnectionResetError, ConnectionAbortedError): full_msg = chatlib.build_message(chatlib.PROTOCOL_CLIENT['logout_msg'], '') if full_msg != '': cmd, data = chatlib.parse_message(full_msg) print(datetime.datetime.now(), '[CLIENT]', conn.getpeername(), full_msg) return cmd, data
def recv_message_and_parse(conn): """ Recieves a new message from given socket. Prints debug info, then parses the message using chatlib. Paramaters: conn (socket object) Returns: cmd (str) and data (str) of the received message. If error occured, will return None, None """ message_gotten = conn.recv(16384).decode() # Debug Info # print("-----------------------------") # print("The Server Sent Us: \n" + message_gotten) # parse the message using chatlib command_code, msg_data = chatlib.parse_message(message_gotten) # check for problems when getting a message if command_code is None or msg_data is None: error_and_exit("An error occurred while trying to get a message headers, None Values") return command_code, msg_data
def recv_message_and_parse(conn): full_msg = conn.recv(1024).decode() cmd, data = chatlib.parse_message(full_msg) return cmd, data
def recv_message_and_parse(conn): full_msg = conn.recv(1024).decode() cmd, data = chatlib.parse_message(full_msg) print_debug_message(DEBG_MSG["client"], conn, full_msg) # Debug print return cmd, data
def recv_message_and_parse(conn): full_msg = conn.recv(MAX_MSG_LENGTH).decode() cmd, data = chatlib.parse_message(full_msg) print("[CLIENT] ", full_msg) # Debug print return cmd, data