-
Notifications
You must be signed in to change notification settings - Fork 3
/
serve-websites.py
executable file
·77 lines (64 loc) · 1.75 KB
/
serve-websites.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
71
72
73
74
75
76
77
#!/usr/bin/env python3
import argparse
import http.server
import multiprocessing
import os
import socketserver
def run_webserver(path, PORT):
os.chdir(path)
Handler = http.server.SimpleHTTPRequestHandler
httpd = socketserver.TCPServer(("0.0.0.0", PORT), Handler)
httpd.serve_forever()
return
def main():
# Change dir to dir of script
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)
# Arguments:
parser = argparse.ArgumentParser(
description="Serve all directories in a folder over http"
)
parser.add_argument(
"--serve-dir",
dest="serve_dir",
help="Directory to serve webpages from",
type=str,
default="./output/final",
)
parser.add_argument(
"--base-port",
dest="base_port",
help="Initial value of http port",
type=int,
default=2000,
)
parser.add_argument(
"--increment-number",
dest="increment_number",
help="Number to increment port num by per webpage",
type=int,
default=100,
)
args = parser.parse_args()
dirs = list(
filter(
lambda item: os.path.isdir(item),
map(lambda item: args.serve_dir + "/" + item, os.listdir(args.serve_dir)),
)
)
servers = []
for dir in dirs:
port = args.base_port + (args.increment_number * dirs.index(dir))
print(f"{dir} will be served on http://127.0.0.1:{port}")
p = multiprocessing.Process(
target=run_webserver,
args=(dir, port),
)
servers.append(p)
for server in servers:
server.start()
for server in servers:
server.join()
if __name__ == "__main__":
main()