Zuletzt aktiv 1697628868

CORS Anywhere Proxy Server in Python

onyx_online's Avatar onyx_online hat die Gist bearbeitet 1697628868. Zu Änderung gehen

1 file changed, 28 insertions

__main__.py(Datei erstellt)

@@ -0,0 +1,28 @@
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()
Neuer Älter