forked from pgh-public-meetings/city-scrapers-pitt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserve.py
34 lines (26 loc) · 1.02 KB
/
serve.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
"""Serve a local directory via a SimpleHTTPRequestHandler
This can be useful for testing the react frontend against
a local spider run.
Usage:
python server.py <spider_output_directory> <port>
"""
import argparse
import socketserver
from http.server import SimpleHTTPRequestHandler
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("directory")
parser.add_argument("port", type=int)
args = parser.parse_args()
directory = args.directory
port = args.port
class Handler(SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=directory, **kwargs)
def end_headers(self):
self.send_header("Access-Control-Allow-Origin", "*")
SimpleHTTPRequestHandler.end_headers(self)
with socketserver.TCPServer(("", port), Handler) as httpd:
url = "http://localhost:{}".format(port)
print("Serving directory '{}' at: {}".format(directory, url))
httpd.serve_forever()