-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.py
executable file
·70 lines (49 loc) · 2.04 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
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
#!/usr/bin/env python3
import argparse
from pathlib import Path
from os import chdir, getcwd, path
from http.server import SimpleHTTPRequestHandler
from socketserver import TCPServer
def dir_path(p):
expandedPath = path.normpath(path.join(getcwd(), p)) if not path.isabs(p) else p
if path.isdir(expandedPath):
return expandedPath
else:
raise argparse.ArgumentTypeError(f"readable_dir:{expandedPath} is not a valid path")
parser = argparse.ArgumentParser(description='Start the RNBO Runner Panel HTTP Server')
parser.add_argument('--port', type=int, default=3000,
help='The port to listen on')
parser.add_argument('--directory', type=dir_path, required=True,
help='The directory to serve the web content from')
args = parser.parse_args()
print(f'RNBO Runner Panel Serving from {args.directory}')
print(f'RNBO Runner Panel Serving on port {args.port}')
# Change to provided directory
chdir(args.directory)
class MyRequestHandler(SimpleHTTPRequestHandler):
def end_headers(self):
self.send_header("Cache-Control", "no-cache, no-store, must-revalidate")
self.send_header("Pragma", "no-cache")
self.send_header("Expires", "0")
super().end_headers()
def do_GET(self):
fext = path.splitext(self.path)[1]
normPath = path.normpath(self.path).split('/')
normPath.remove('')
fpath = path.join(args.directory, '{os.sep}'.join(normPath))
# handle /instances/12 -> /instances/[index].html mapping
ipath = path.join(normPath[0], "[index].html")
if not path.isfile(fpath) and not fext and path.isfile(fpath + '.html'):
self.path = self.path + '.html'
elif len(normPath) == 2 and path.isfile(path.join(args.directory, ipath)):
self.path = path.join("/", ipath)
return SimpleHTTPRequestHandler.do_GET(self)
Handler = MyRequestHandler
# if we restart the socket is sometimes still bound
# https://stackoverflow.com/questions/19071512/socket-error-errno-48-address-already-in-use
TCPServer.allow_reuse_address=True
server = TCPServer(('0.0.0.0', args.port), Handler)
try:
server.serve_forever()
finally:
server.server_close()