-
Notifications
You must be signed in to change notification settings - Fork 2
/
web_server.py
154 lines (139 loc) · 5.38 KB
/
web_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import network
import usocket
import _thread
from os import stat, listdir
from time import sleep_ms
class WebServer:
def __init__(self, web_folder='/www', port=80):
self.WEB_FOLDER = web_folder
self.MIMETYPES = {
"txt" : "text/plain",
"htm" : "text/html",
"html" : "text/html",
"css" : "text/css",
"csv" : "text/csv",
"js" : "application/javascript",
"xml" : "application/xml",
"xhtml" : "application/xhtml+xml",
"json" : "application/json",
"zip" : "application/zip",
"pdf" : "application/pdf",
"ts" : "application/typescript",
"ttf" : "font/ttf",
"jpg" : "image/jpeg",
"jpeg" : "image/jpeg",
"png" : "image/png",
"gif" : "image/gif",
"svg" : "image/svg+xml",
"ico" : "image/x-icon",
"cur" : "application/octet-stream",
"tar" : "application/tar",
"tar.gz": "application/tar+gzip",
"gz" : "application/gzip",
"mp3" : "audio/mpeg",
"wav" : "audio/wav",
"ogg" : "audio/ogg"
}
self.webserv_sock = None
self.url_handlers = {}
self.port = port
def _file_exists(self, path):
try:
stat(path)
return True
except:
return False
def get_mime_type(self, filename):
try:
_, ext = filename.rsplit(".", 1)
return self.MIMETYPES.get(ext, "application/octet-stream")
except:
return "application/octet-stream"
def read_in_chunks(self, file_object, chunk_size=1024):
while True:
data = file_object.read(chunk_size)
if not data:
break
yield data
def serve_file(self, client, path):
try:
if path.startswith("/*GET_FILE"):
file_path = path.replace("/*GET_FILE", "")
else:
if path == "/":
path = "/index.html"
file_path = self.WEB_FOLDER + path
mime_type = self.get_mime_type(file_path)
filestatus = 0 # 0=Not found 1=Found 2=found in GZip
if self._file_exists(file_path + '.gz'):
filestatus = 2
file_path += '.gz'
elif self._file_exists(file_path):
filestatus = 1
if filestatus > 0:
with open(file_path, 'rb') as file:
client.write(b'HTTP/1.1 200 OK\r\n')
client.write(b"Content-Type: " + mime_type.encode() + b"\r\n")
if filestatus == 2:
client.write(b'Content-Encoding: gzip\r\n')
client.write(b'\r\n')
for piece in self.read_in_chunks(file):
client.write(piece)
else:
client.write(b"HTTP/1.0 404 Not Found\r\n\r\nFile not found.")
except OSError as e:
print("OSError:", e)
client.write(b"HTTP/1.0 500 Internal Server Error\r\n\r\nInternal error.")
except Exception as e:
print("Exception:", e)
client.write(b"HTTP/1.0 500 Internal Server Error\r\n\r\nInternal error.")
def handle(self, pattern):
"""Decorator to register a handler for a specific URL pattern."""
def decorator(func):
self.url_handlers[pattern] = func
return func
return decorator
def client_handler(self, client):
try:
request = client.recv(2048)
if request:
_, path, _ = request.decode("utf-8").split(" ", 2)
for pattern, handler in self.url_handlers.items():
if path.startswith(pattern):
try:
handler(client, path, request)
except Exception as e:
print("Handler Exception:", e)
client.close()
return
# Default file serving if no handler matches
self.serve_file(client, path)
except Exception as e:
sleep_ms(0)
#print("Webserver Exception:", e)
finally:
client.close()
def web_thread(self):
while True:
try:
cl, addr = self.webserv_sock.accept()
cl.settimeout(2) # time in seconds
self.client_handler(cl)
except Exception as ex:
sleep_ms(0)
def start(self):
addr = usocket.getaddrinfo('0.0.0.0', self.port)[0][-1]
self.webserv_sock = usocket.socket()
self.webserv_sock.setsockopt(usocket.SOL_SOCKET, usocket.SO_REUSEADDR, 1)
self.webserv_sock.bind(addr)
self.webserv_sock.listen(5)
_thread.start_new_thread(self.web_thread, ())
for interface in [network.AP_IF, network.STA_IF]:
wlan = network.WLAN(interface)
if not wlan.active():
continue
ifconfig = wlan.ifconfig()
print("Web server spusten na adrese {}:{}".format(ifconfig[0], self.port))
def stop(self):
if self.webserv_sock:
self.webserv_sock.close()