__main__.py
· 914 B · Python
原始檔案
import argparse
from http.server import HTTPServer, SimpleHTTPRequestHandler
parser = argparse.ArgumentParser(
prog="Simple HTTP Server",
description="Serves static files using Python3 Simple HTTP module",
)
parser.add_argument("-d", "--directory", required=True)
args = parser.parse_args()
PORT = 8000
DIRECTORY = args.directory
class CORSRequestHandler(SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=DIRECTORY, **kwargs)
def end_headers(self):
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Access-Control-Allow-Methods", "GET")
self.send_header("Cache-Control", "no-store, no-cache, must-revalidate")
return super(CORSRequestHandler, self).end_headers()
with HTTPServer(("", PORT), CORSRequestHandler) as httpd:
print("serving at port:", PORT)
httpd.serve_forever()
1 | import argparse |
2 | from http.server import HTTPServer, SimpleHTTPRequestHandler |
3 | |
4 | parser = argparse.ArgumentParser( |
5 | prog="Simple HTTP Server", |
6 | description="Serves static files using Python3 Simple HTTP module", |
7 | ) |
8 | parser.add_argument("-d", "--directory", required=True) |
9 | args = parser.parse_args() |
10 | |
11 | PORT = 8000 |
12 | DIRECTORY = args.directory |
13 | |
14 | |
15 | class CORSRequestHandler(SimpleHTTPRequestHandler): |
16 | def __init__(self, *args, **kwargs): |
17 | super().__init__(*args, directory=DIRECTORY, **kwargs) |
18 | |
19 | def end_headers(self): |
20 | self.send_header("Access-Control-Allow-Origin", "*") |
21 | self.send_header("Access-Control-Allow-Methods", "GET") |
22 | self.send_header("Cache-Control", "no-store, no-cache, must-revalidate") |
23 | return super(CORSRequestHandler, self).end_headers() |
24 | |
25 | |
26 | with HTTPServer(("", PORT), CORSRequestHandler) as httpd: |
27 | print("serving at port:", PORT) |
28 | httpd.serve_forever() |