-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·39 lines (30 loc) · 1.11 KB
/
server.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
#!/usr/bin/env python3.7
import http.server
import os
import re
from pathlib import Path
import sys
class MyHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
def end_headers(self):
self.send_my_headers()
http.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")
def do_GET(self):
path, query = re.match(r'([^?]+)(\?.*)?', self.path).groups()
# If they're fetching the root, then run `make` first.
if path == '/':
os.system('make')
return super().do_GET()
if __name__ == '__main__':
# I'm "whatever/kasuba/bin/server.py", and I want to cd to
# "whatever/kasuba". That is, unless an argument was passed specifying
# from where to serve files.
if len(sys.argv) > 1:
os.chdir(sys.argv[1])
else:
repo = Path(sys.argv[0]).resolve().parent.parent
os.chdir(repo)
http.server.test(HandlerClass=MyHTTPRequestHandler)