From eca248c50b1fd1a030a1df9fb094640a34bd2bfe Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 11 Jun 2019 01:37:20 +0400 Subject: =?UTF-8?q?=D0=A3=D0=B1=D1=80=D0=B0=D0=BB=20=D0=BB=D0=B8=D1=88?= =?UTF-8?q?=D0=BD=D0=B8=D0=B9=20=D0=BA=D0=BE=D0=B4,=20=D0=BF=D0=B5=D1=80?= =?UTF-8?q?=D0=B5=D0=BC=D0=B5=D1=81=D1=82=D0=B8=D0=BB=20=D0=B0=D0=B4=D1=80?= =?UTF-8?q?=D0=B5=D1=81=20=D0=BA=D0=BB=D0=B8=D0=B5=D0=BD=D1=82=D0=B0=20?= =?UTF-8?q?=D0=B2=20=D1=82=D0=B5=D0=BB=D0=BE=20=D0=B7=D0=B0=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D1=81=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day7/http_handler.py | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/day7/http_handler.py b/day7/http_handler.py index 67769d4..f764551 100644 --- a/day7/http_handler.py +++ b/day7/http_handler.py @@ -23,7 +23,7 @@ def log_requests(func): Логирует в файл все запросы, приходящие в функцию `process_request`. """ - def wrapper(address, request, *args, **kwargs): + def wrapper(request, *args, **kwargs): response = func(*args, **kwargs) status = response.split('\n').pop(0).split()[1] @@ -51,7 +51,7 @@ def log_requests(func): to_logger = f'Cookies: {format_cookies(request["cookies"])} ' log_line = ( - f'{address[0]}: {strftime("[%d %b %Y %H:%M:%S]", gmtime())} ' + f'{request["address"]}: {strftime("[%d %b %Y %H:%M:%S]", gmtime())} ' f'"{request["method"]} {request["url"]} {request["http_ver"]}"' f' | {to_logger} | ' f'Status: {status}' @@ -66,7 +66,7 @@ def log_requests(func): @log_requests -def process_request(address, request: dict): +def process_request(request: dict): """ *Эта функция логируется с помощью функции* `log_func` @@ -146,6 +146,7 @@ def handle_connection(connection, address): try: request = parse_request(connection) + request['address'] = address[0] except ValueError as e: response = add_text_headers(BAD_REQUEST, render_template( TEXT_TEMPLATE_NAME, color=get_color(), @@ -157,7 +158,7 @@ def handle_connection(connection, address): if request is None: return - response = process_request(address, request) + response = process_request(request) connection.sendall(response.encode()) @@ -188,19 +189,10 @@ def main(): connection.shutdown(SHUT_RDWR) connection.close() - del connection except (KeyboardInterrupt, ConnectionError): print('Stopping server...') - try: - connection.shutdown(SHUT_RDWR) - connection.close() - except UnboundLocalError: - # Если переменная connection еще не создана, то нам не нужно закрывать - # соединение, а значит можно игнорировать это исключение. - pass - sock.shutdown(SHUT_RDWR) sock.close() -- cgit v1.2.3