-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
40 lines (32 loc) · 1.25 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
import os
import http.server
import socketserver
class MyHandler(http.server.SimpleHTTPRequestHandler):
def translate_path(self, path):
# get the original translated path
path = super().translate_path(path)
# if it's a directory, return 404
if os.path.isdir(path):
self.send_error(404, "File not found")
return None
# only allow access to files with certain extensions
allowed_extensions = ['.html', '.js', '.css', '.json', ',ico']
if not any(path.endswith(ext) for ext in allowed_extensions):
self.send_error(404, "File not found")
return None
# return the original path if it passed all the tests
return path
with open("config.txt") as f:
for line in f:
line = line.strip()
if line.startswith("port_number="):
port_number = int(line.split("=")[1])
Handler = MyHandler
class MyServer(socketserver.TCPServer):
def __init__(self, server_address, HandlerClass):
super().__init__(server_address, HandlerClass)
self.allow_reuse_address = True
self.allow_cross_domain = True
with MyServer(("", port_number), Handler) as httpd:
print("\nserving at port", port_number)
httpd.serve_forever()