-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
23 lines (20 loc) · 766 Bytes
/
server.py
File metadata and controls
23 lines (20 loc) · 766 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import http.server
import socketserver
PORT = 8000
class CustomHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
if self.path.endswith(".html"):
try:
with open(self.path[1:], "rb") as f:
content = f.read()
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(content)
except FileNotFoundError:
self.send_error(404, "File not found")
else:
super().do_GET()
with socketserver.TCPServer(("", PORT), CustomHandler) as httpd:
print(f"Serving at port {PORT}")
httpd.serve_forever()