def handle_request(client_sock, server_addr: str, server_port: int): try: # receive data from client(i.e. browser) head_data = client_sock.recv(BUFFER_SIZE) if not head_data: client_sock.close() return parse_head(head_data) # show debug message encrypted_data = encrypt(head_data) # encrypt data target_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # send encrypted data to server target_sock.connect((server_addr, server_port)) target_sock.send(encrypted_data) read_write(client_sock, target_sock) # async communication target_sock.close() # close socket except TimeoutError: logging.warning('[WARN] [{}] {:>61}'.format(get_time_str(), "link to " + server_addr + ' time out.')) except ConnectionAbortedError: logging.warning('[WARN] [{}] {:>61}'.format(get_time_str(), "link to " + server_addr + ' was aborted by client.')) except ConnectionResetError: logging.warning('[WARN] [{}] {:>61}'.format(get_time_str(), "link to " + server_addr + ' was reseted.')) except ConnectionRefusedError: logging.warning('[WARN] [{}] {:>61}'.format(get_time_str(), "link to " + server_addr + ' was refused.')) except socket.gaierror: logging.error('[ERR] [{}] {:>61}'.format(get_time_str(), "can't CONNECT to server!")) finally: client_sock.close()
def handle_request(client_sock): try: head_str = decrypt(client_sock.recv(BUFFER_SIZE)) # receive data from client method, path, protocol = parse_head(head_str) # analyze data target_sock = _get_target_sock(method, path, client_sock, head_str) read_write(client_sock, target_sock) # async communication except TimeoutError: logging.warning("[WARN] [{}] {:7} {:>53}".format(get_time_str(), method, short_str(path, 31) + " time out.")) except ConnectionAbortedError: logging.warning( "[WARN] [{}] {:7} {:>53}".format(get_time_str(), method, short_str(path, 22) + " aborted by client.") ) except ConnectionResetError: logging.warning("[WARN] [{}] {:7} {:>53}".format(get_time_str(), method, short_str(path, 28) + " was reseted.")) except ConnectionRefusedError: logging.warning("[WARN] [{}] {:7} {:>53}".format(get_time_str(), method, short_str(path, 28) + " was refused.")) except socket.gaierror: logging.error("[ERR ] [{}] {:>53}".format(get_time_str(), "can't CONNECT to server!")) finally: client_sock.close()