-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
42 lines (34 loc) · 1.3 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
import os
import argparse
from http.server import HTTPServer, SimpleHTTPRequestHandler
class CORSHTTPRequestHandler(SimpleHTTPRequestHandler):
def end_headers(self):
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Access-Control-Allow-Methods", "GET, OPTIONS")
self.send_header("Access-Control-Allow-Headers", "*")
super().end_headers()
def do_OPTIONS(self):
self.send_response(200, "OK")
self.end_headers()
def run(
server_class=HTTPServer,
handler_class=CORSHTTPRequestHandler,
port=8000,
directory=None,
):
if directory: # Change the current working directory if directory is specified
os.chdir(directory)
server_address = ("", port)
httpd = server_class(server_address, handler_class)
print(
f"Serving HTTP on http://localhost:{port} from directory '{directory}'...")
httpd.serve_forever()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="HTTP Server with CORS")
parser.add_argument(
"--dir", type=str, help="Directory to serve files from", default="dist"
)
parser.add_argument("--port", type=int,
help="Port to serve HTTP on", default=8888)
args = parser.parse_args()
run(port=args.port, directory=args.dir)