From c9ed7835c1c3eec07a84134f589c3c4e9033778b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B2=D0=B0=D0=BD=20=D0=A1=D0=BE=D0=BB=D0=BD=D1=86?= =?UTF-8?q?=D0=B5=D0=B2?= Date: Sat, 5 Oct 2024 17:27:06 +0300 Subject: [PATCH] Move log setup functional ... to main file. Fix: - HTTP handler: - Rename function __write_func into http handler file; - Change close connection functions to self.__close(). --- config.py | 10 ---------- main.py | 12 +++++++++++- server/http_handler.py | 11 ++++------- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/config.py b/config.py index dbde6b0..eead487 100644 --- a/config.py +++ b/config.py @@ -16,16 +16,6 @@ SETUP = { } } - -if SETUP["setup"]["log_to_file"]: - logging.basicConfig(filename=os.path.join(os.getcwd(), "log", "main.log"), filemode="a", encoding="UTF-8", - level=SETUP["setup"]["log_level"], - format="[%(asctime)s][%(levelname)s][%(name)s] %(message)s") -else: - logging.basicConfig(encoding="UTF-8", level=SETUP["setup"]["log_level"], - format="[%(asctime)s][%(levelname)s][%(name)s] %(message)s") - - urls = [ Path("/", HandlerType.STATIC_FILE, "index.html"), Path("/func", HandlerType.FUNCTION, testmod.work, HTTPMethod.GET|HTTPMethod.POST), diff --git a/main.py b/main.py index 8acb773..87b3b83 100755 --- a/main.py +++ b/main.py @@ -2,13 +2,23 @@ import os import logging +import config + from server.main import init_server_socket from server.http_handler import HTTPHandler -import config log = logging.getLogger(__name__) +if config.SETUP["setup"]["log_to_file"]: + logging.basicConfig(filename=os.path.join(os.getcwd(), "log", "main.log"), filemode="a", encoding="UTF-8", + level=config.SETUP["setup"]["log_level"], + format="[%(asctime)s][%(levelname)s][%(name)s] %(message)s") +else: + logging.basicConfig(encoding="UTF-8", level=config.SETUP["setup"]["log_level"], + format="[%(asctime)s][%(levelname)s][%(name)s] %(message)s") + + if __name__ == "__main__": try: os.mkdir(os.path.join(os.getcwd(), "log")) diff --git a/server/http_handler.py b/server/http_handler.py index 1526dee..5a8c4d9 100644 --- a/server/http_handler.py +++ b/server/http_handler.py @@ -165,14 +165,12 @@ class HTTPHandler: if len(raw) > MAX_REQUEST_LINE_SIZE: log.debug("Request header line too long") - self.conn.send(b"") - self.conn.close() + self.__close() return if raw == b"": log.debug("Client not send data, close connection") - self.conn.send(b"") - self.conn.close() + self.__close() return if raw == b"\r\n" or raw == b"\n": @@ -201,8 +199,7 @@ class HTTPHandler: bytes_to_receive = int(self.http_headers["Content-Length"]) else: log.error(" Content-Length is not integer") - self.conn.send(b"") - self.conn.close() + self.__close() return log.debug("Want to receive {} bytes from client".format(bytes_to_receive)) @@ -259,6 +256,6 @@ class HTTPHandler: if not found: r = Response(status_code=404, data=b"Not found!") - self.__write_func(r) + self.__write_data(r) self.__close()