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()