-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·127 lines (111 loc) · 4.09 KB
/
Copy pathserver.py
File metadata and controls
executable file
·127 lines (111 loc) · 4.09 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python3
"""Dev HTTPS server for Mochi local development.
Usage (via invoke):
inv serve
Direct usage:
./server.py [--bind 0.0.0.0] [--port 8443] [--cert F] [--key F] [--host DISPLAY] [--directory DIR]
Flag defaults come from a gitignored `.env` file when present (see
`.env.example`), so a bare `./server.py` serves the same address as
`inv serve`.
"""
import argparse
import errno
import functools
import http.server
import os
import ssl
import subprocess
def _load_env(path=".env"):
"""Read KEY=VALUE lines into os.environ without overriding real env vars."""
try:
with open(path) as fh:
for line in fh:
line = line.strip()
if not line or line.startswith("#") or "=" not in line:
continue
key, _, value = line.partition("=")
os.environ.setdefault(key.strip(), value.strip())
except FileNotFoundError:
pass
class _NoCacheHandler(http.server.SimpleHTTPRequestHandler):
"""SimpleHTTPRequestHandler that disables client caching.
Without this, Chrome heuristically caches HTML and CSS, so edits to
`index.html` (or anything else) only show up after a hard reload. In
dev that's confusing — clobber every cached response so a normal
refresh always picks up the latest file.
"""
def end_headers(self):
self.send_header("Cache-Control", "no-store, must-revalidate")
self.send_header("Pragma", "no-cache")
self.send_header("Expires", "0")
super().end_headers()
def _port_owner(port):
try:
out = subprocess.check_output(
["ss", "-tlnp", f"sport = :{port}"],
stderr=subprocess.DEVNULL,
text=True,
)
for line in out.splitlines():
if f":{port}" in line and "users:" in line:
return line.strip()
except Exception:
pass
try:
out = subprocess.check_output(
["lsof", "-nP", "-iTCP", f"-i:{port}", "-sTCP:LISTEN"],
stderr=subprocess.DEVNULL,
text=True,
)
lines = [l for l in out.splitlines() if l and not l.startswith("COMMAND")]
if lines:
return lines[0]
except Exception:
pass
return None
def run(bind, port, certfile=None, keyfile=None, display_host=None, directory="app"):
handler = functools.partial(_NoCacheHandler, directory=directory)
try:
httpd = http.server.ThreadingHTTPServer((bind, port), handler)
except OSError as exc:
if exc.errno == errno.EADDRINUSE:
info = _port_owner(port)
msg = f"error: port {port} is already in use."
if info:
msg += f"\n {info}"
raise SystemExit(msg) from None
raise
if certfile and keyfile:
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ctx.minimum_version = ssl.TLSVersion.TLSv1_2
ctx.load_cert_chain(certfile, keyfile)
httpd.socket = ctx.wrap_socket(httpd.socket, server_side=True)
scheme = "https"
else:
scheme = "http"
host = display_host or (bind if bind != "0.0.0.0" else "localhost")
print(f"Serving {scheme}://{host}:{port}/ from {directory}/")
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
finally:
httpd.server_close()
print("\nServer stopped.")
if __name__ == "__main__":
_load_env()
ap = argparse.ArgumentParser(description=__doc__)
ap.add_argument("--bind", default=os.environ.get("MOCHI_BIND", "127.0.0.1"))
ap.add_argument("--port", type=int, default=int(os.environ.get("MOCHI_PORT", "8443")))
ap.add_argument("--cert", default=os.environ.get("MOCHI_CERT"))
ap.add_argument("--key", default=os.environ.get("MOCHI_KEY"))
ap.add_argument(
"--host",
default=os.environ.get("MOCHI_HOST"),
help="Display hostname in the startup URL",
)
ap.add_argument(
"--directory", default="app", help="Directory to serve (default: app)"
)
args = ap.parse_args()
run(args.bind, args.port, args.cert, args.key, args.host, args.directory)