-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathserver.py
More file actions
32 lines (26 loc) · 1023 Bytes
/
server.py
File metadata and controls
32 lines (26 loc) · 1023 Bytes
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
#!/usr/bin/env python3
import http.server
import socketserver
import mimetypes
import os
PORT = 8000
class GzipStaticHandler(http.server.SimpleHTTPRequestHandler):
def end_headers(self):
# If the requested file ends with .gz, add Content-Encoding
if self.path.endswith(".gz"):
self.send_header("Content-Encoding", "gzip")
# Try to infer the original MIME type (e.g., .js.gz -> .js)
base, _ = os.path.splitext(self.path)
mime, _ = mimetypes.guess_type(base)
if mime:
self.send_header("Content-Type", mime)
super().end_headers()
if __name__ == "__main__":
socketserver.TCPServer.allow_reuse_address = True
with socketserver.TCPServer(("", PORT), GzipStaticHandler) as httpd:
print(f"Serving HTTP on port {PORT} (http://localhost:{PORT}/)")
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nShutting down server.")
httpd.server_close()