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().
This commit is contained in:
Иван Солнцев 2024-10-05 17:27:06 +03:00
parent d356a80685
commit c9ed7835c1
Signed by: johnsol
GPG key ID: C56B337609C9821E
3 changed files with 15 additions and 18 deletions

View file

@ -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),

12
main.py
View file

@ -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"))

View file

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