最後活躍 1697628868

CORS Anywhere Proxy Server in Python

__main__.py 原始檔案
1import argparse
2from http.server import HTTPServer, SimpleHTTPRequestHandler
3
4parser = argparse.ArgumentParser(
5 prog="Simple HTTP Server",
6 description="Serves static files using Python3 Simple HTTP module",
7)
8parser.add_argument("-d", "--directory", required=True)
9args = parser.parse_args()
10
11PORT = 8000
12DIRECTORY = args.directory
13
14
15class 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
26with HTTPServer(("", PORT), CORSRequestHandler) as httpd:
27 print("serving at port:", PORT)
28 httpd.serve_forever()