Skip to content

Instantly share code, notes, and snippets.

@stevenhao
Created June 18, 2019 21:24
Show Gist options
  • Save stevenhao/f1c32870e1cd40adcebe18b00b7fc41c to your computer and use it in GitHub Desktop.
Save stevenhao/f1c32870e1cd40adcebe18b00b7fc41c to your computer and use it in GitHub Desktop.
ss.py
#!/usr/bin/env python3
# encoding: utf-8
"""Use instead of `python3 -m http.server` when you need CORS"""
import sys
from http.server import HTTPServer, SimpleHTTPRequestHandler
class CORSRequestHandler(SimpleHTTPRequestHandler):
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()
if len(sys.argv) >= 2:
PORT = int(sys.argv[1])
else:
PORT = 8000
httpd = HTTPServer(('localhost', PORT), CORSRequestHandler)
print('Serving HTTP on 0.0.0.0 port {} (http://localhost:{}/) ...'.format(PORT, PORT))
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment