-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve.py
More file actions
45 lines (38 loc) · 1.43 KB
/
serve.py
File metadata and controls
45 lines (38 loc) · 1.43 KB
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
#!/usr/bin/env python3
"""
Local dev server that mimics GitHub Pages 404 behaviour.
Usage: python3 serve.py [port] (default port: 8001)
"""
import http.server
import socketserver
import sys
import os
PORT = int(sys.argv[1]) if len(sys.argv) > 1 else 8001
ROOT = os.path.dirname(os.path.abspath(__file__))
class GHPagesHandler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=ROOT, **kwargs)
def send_error(self, code, message=None, explain=None):
if code == 404:
page = os.path.join(ROOT, "404.html")
try:
with open(page, "rb") as f:
body = f.read()
self.send_response(404)
self.send_header("Content-Type", "text/html; charset=utf-8")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
return
except FileNotFoundError:
pass
super().send_error(code, message, explain)
def log_message(self, fmt, *args):
print(f" {self.address_string()} — {fmt % args}")
with socketserver.TCPServer(("", PORT), GHPagesHandler) as httpd:
httpd.allow_reuse_address = True
print(f"Serving at http://0.0.0.0:{PORT} (Ctrl+C to stop)")
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nStopped.")