PYTHON   33
SimpleHTTPRequestHandler
Guest on 18th September 2023 12:13:46 PM


  1. #!/usr/bin/env python
  2.  
  3. # Attribution: https://stackoverflow.com/questions/21956683/enable-access-control-on-simple-http-server
  4.  
  5. try:
  6.     # Python 3
  7.     from http.server import HTTPServer, SimpleHTTPRequestHandler, test as test_orig
  8.     import sys
  9.     def test (*args):
  10.         test_orig(*args, port=int(sys.argv[1]) if len(sys.argv) > 1 else 8000)
  11. except ImportError: # Python 2
  12.     from BaseHTTPServer import HTTPServer, test
  13.     from SimpleHTTPServer import SimpleHTTPRequestHandler
  14.  
  15. class CORSRequestHandler (SimpleHTTPRequestHandler):
  16.     def end_headers (self):
  17.         self.send_header('Access-Control-Allow-Origin', '*')
  18.         self.send_header('Access-Control-Allow-Private-Network', 'true')
  19.         SimpleHTTPRequestHandler.end_headers(self)
  20.  
  21. if __name__ == '__main__':
  22.     test(CORSRequestHandler, HTTPServer)

Raw Paste

Login or Register to edit or fork this paste. It's free.