forked from CycodeLabs/simple-http-logger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
79 lines (62 loc) · 2.1 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import sys
from http.server import BaseHTTPRequestHandler, HTTPServer
def print_in_color(text: str, color: str, end="\n"):
HEADER = "\033[95m"
OKBLUE = "\033[94m"
OKCYAN = "\033[96m"
OKGREEN = "\033[92m"
WARNING = "\033[93m"
FAIL = "\033[91m"
ENDC = "\033[0m"
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
if color == "green":
text = OKGREEN + text + ENDC
elif color == "cyan":
text = OKCYAN + text + ENDC
elif color == "blue":
text = OKBLUE + text + ENDC
elif color == "brown":
text = WARNING + text + ENDC
print(text, end=end)
class Server(BaseHTTPRequestHandler):
def _set_response(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
def do_GET(self):
print_in_color(f"{self.headers}", end="", color="green")
self._set_response()
self.wfile.write("GET request for {}".format(self.path).encode("utf-8"))
def do_POST(self):
if self.headers["Content-Length"]:
content_length = int(self.headers["Content-Length"])
post_data = self.rfile.read(content_length)
else:
post_data = b""
print_in_color(f"{self.headers}", color="green", end="")
print_in_color(f"{post_data.decode('utf-8')}\n", color="cyan")
self._set_response()
self.wfile.write("POST request for {}".format(self.path).encode("utf-8"))
def log_message(self, format, *args):
print_in_color(
"%s - - [%s] %s\n"
% (self.address_string(), self.log_date_time_string(), format % args),
color="brown",
)
def run(server_class=HTTPServer, handler_class=Server, port=8080):
server_address = ("", port)
httpd = server_class(server_address, handler_class)
print("Starting httpd...\n")
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
print("Stopping httpd...\n")
if __name__ == "__main__":
from sys import argv
if len(argv) == 2:
run(port=int(argv[1]))
else:
run()