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:
parent
d356a80685
commit
c9ed7835c1
3 changed files with 15 additions and 18 deletions
10
config.py
10
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 = [
|
urls = [
|
||||||
Path("/", HandlerType.STATIC_FILE, "index.html"),
|
Path("/", HandlerType.STATIC_FILE, "index.html"),
|
||||||
Path("/func", HandlerType.FUNCTION, testmod.work, HTTPMethod.GET|HTTPMethod.POST),
|
Path("/func", HandlerType.FUNCTION, testmod.work, HTTPMethod.GET|HTTPMethod.POST),
|
||||||
|
|
12
main.py
12
main.py
|
@ -2,13 +2,23 @@
|
||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
import config
|
||||||
|
|
||||||
from server.main import init_server_socket
|
from server.main import init_server_socket
|
||||||
from server.http_handler import HTTPHandler
|
from server.http_handler import HTTPHandler
|
||||||
import config
|
|
||||||
|
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
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__":
|
if __name__ == "__main__":
|
||||||
try:
|
try:
|
||||||
os.mkdir(os.path.join(os.getcwd(), "log"))
|
os.mkdir(os.path.join(os.getcwd(), "log"))
|
||||||
|
|
|
@ -165,14 +165,12 @@ class HTTPHandler:
|
||||||
|
|
||||||
if len(raw) > MAX_REQUEST_LINE_SIZE:
|
if len(raw) > MAX_REQUEST_LINE_SIZE:
|
||||||
log.debug("Request header line too long")
|
log.debug("Request header line too long")
|
||||||
self.conn.send(b"")
|
self.__close()
|
||||||
self.conn.close()
|
|
||||||
return
|
return
|
||||||
|
|
||||||
if raw == b"":
|
if raw == b"":
|
||||||
log.debug("Client not send data, close connection")
|
log.debug("Client not send data, close connection")
|
||||||
self.conn.send(b"")
|
self.__close()
|
||||||
self.conn.close()
|
|
||||||
return
|
return
|
||||||
|
|
||||||
if raw == b"\r\n" or raw == b"\n":
|
if raw == b"\r\n" or raw == b"\n":
|
||||||
|
@ -201,8 +199,7 @@ class HTTPHandler:
|
||||||
bytes_to_receive = int(self.http_headers["Content-Length"])
|
bytes_to_receive = int(self.http_headers["Content-Length"])
|
||||||
else:
|
else:
|
||||||
log.error(" Content-Length is not integer")
|
log.error(" Content-Length is not integer")
|
||||||
self.conn.send(b"")
|
self.__close()
|
||||||
self.conn.close()
|
|
||||||
return
|
return
|
||||||
|
|
||||||
log.debug("Want to receive {} bytes from client".format(bytes_to_receive))
|
log.debug("Want to receive {} bytes from client".format(bytes_to_receive))
|
||||||
|
@ -259,6 +256,6 @@ class HTTPHandler:
|
||||||
|
|
||||||
if not found:
|
if not found:
|
||||||
r = Response(status_code=404, data=b"Not found!")
|
r = Response(status_code=404, data=b"Not found!")
|
||||||
self.__write_func(r)
|
self.__write_data(r)
|
||||||
|
|
||||||
self.__close()
|
self.__close()
|
||||||
|
|
Loading…
Reference in a new issue