Пример #1
0
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()
Пример #2
0
def parse_head(head_str: bytes):
    method, path, protocol = head_str.split(b'\r\n')[0].split(b' ')
    # assert str(type(method)) == "<class 'bytes'>"
    # assert str(type(protocol)) == "<class 'bytes'>"
    # assert str(type(path)) == "<class 'bytes'>"
    method = method.decode()
    path = path.decode()
    protocol = protocol.decode()
    # assert str(type(method)) == "<class 'str'>"
    # assert str(type(path)) == "<class 'str'>"
    # assert str(type(protocol)) == "<class 'str'>"
    logging.info('[INFO] [{}] {:7} {:44} {}'.format(get_time_str(), method, short_str(path, PATH_LEN), protocol))
    logging.debug('[DEBUG] [{} in {} running threads]'.format(threading.current_thread().getName(), threading.active_count()))
    logging.debug(head_str)
    logging.debug('')
    return method, path, protocol