-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathserve_examples.py
48 lines (37 loc) · 1.57 KB
/
serve_examples.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
import argparse
import os
import pathlib
import sys
from http import server
project_path = pathlib.Path(__file__).parent
puepy_path = project_path / "puepy"
examples_path = project_path / "examples"
assert project_path.is_dir()
assert puepy_path.is_dir()
assert (puepy_path / "core.py").exists()
sys.path.append(str(project_path))
class Handler(server.SimpleHTTPRequestHandler):
def do_GET(self):
path = self.path.split("?")[0]
# We want to serve the examples from the /examples directory, but also allow pulls from /puepy/ for live
# changes to code. This basically just treats /examples as the content root, unless the path starts with /puepy/
if not path.startswith("/puepy/"):
self.path = f"/examples{path}"
return super().do_GET()
def end_headers(self):
"""
Cache nothing!
"""
self.send_header("Cache-Control", "no-store, no-cache, must-revalidate")
self.send_header("Pragma", "no-cache")
self.send_header("Expires", "0")
super().end_headers()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="A simple HTTP server to serve examples of PuePy")
parser.add_argument("--host", default="", help="The host on which the server runs")
parser.add_argument("--port", type=int, default=8000, help="The port on which the server listens")
args = parser.parse_args()
os.chdir(project_path)
httpd = server.ThreadingHTTPServer((args.host, args.port), Handler)
print(f"Serving at port {args.host or '*'}:{args.port}")
httpd.serve_forever()