-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathserver.py
More file actions
76 lines (64 loc) · 2.15 KB
/
server.py
File metadata and controls
76 lines (64 loc) · 2.15 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
#!/usr/bin/env python3
# This wrapper turns off the Python HTTP server's overly aggressive
# cache headers, which can get in the way of Verso hovers.
from http import server # Python 3
from http.server import ThreadingHTTPServer, test
import os
class NonCachingHTTPRequestHandler(server.SimpleHTTPRequestHandler):
def end_headers(self):
self.send_my_headers()
server.SimpleHTTPRequestHandler.end_headers(self)
def send_my_headers(self):
self.send_header("Cache-Control", "no-cache, no-store, must-revalidate")
self.send_header("Pragma", "no-cache")
self.send_header("Expires", "0")
if __name__ == "__main__":
import argparse
import contextlib
parser = argparse.ArgumentParser()
parser.add_argument(
"-b",
"--bind",
metavar="ADDRESS",
help="bind to this address (default: all interfaces)",
)
parser.add_argument(
"-d",
"--directory",
default="_out/html-multi",
help="serve this directory (default: _out/html-multi)",
)
parser.add_argument(
"-p",
"--protocol",
metavar="VERSION",
default="HTTP/1.0",
help="conform to this HTTP version (default: %(default)s)",
)
parser.add_argument(
"port",
default=8000,
type=int,
nargs="?",
help="bind to this port (default: %(default)s)",
)
args = parser.parse_args()
# ensure dual-stack is not disabled; ref #38907
class DualStackServer(ThreadingHTTPServer):
def server_bind(self):
# suppress exception when protocol is IPv4
with contextlib.suppress(Exception):
self.socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
return super().server_bind()
def finish_request(self, request, client_address):
self.RequestHandlerClass(
request, client_address, self, directory=args.directory
)
print(args)
test(
HandlerClass=NonCachingHTTPRequestHandler,
ServerClass=DualStackServer,
port=args.port,
bind=args.bind,
protocol=args.protocol,
)