Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save thenoobtester/95e63937f9abef845b6d5eb1c46a3b80 to your computer and use it in GitHub Desktop.
Save thenoobtester/95e63937f9abef845b6d5eb1c46a3b80 to your computer and use it in GitHub Desktop.
Python SimpleHTTPServer with CORS
#!/usr/bin/env python
# Usage: python cors_http_server.py <port>
try:
# try to use Python 3
from http.server import HTTPServer, SimpleHTTPRequestHandler, test as test_orig
import sys
def test (*args):
test_orig(*args, port=int(sys.argv[1]) if len(sys.argv) > 1 else 8000)
except ImportError: # fall back to Python 2
from BaseHTTPServer import HTTPServer, test
from SimpleHTTPServer import SimpleHTTPRequestHandler
class CORSRequestHandler (SimpleHTTPRequestHandler):
def end_headers (self):
self.send_header('Access-Control-Allow-Origin', '*')
SimpleHTTPRequestHandler.end_headers(self)
if __name__ == '__main__':
test(CORSRequestHandler, HTTPServer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment