May 4, 2026

Python ships on MacOS, which makes it very handy. Sometimes I need a quick, simple web server to work from. This script lets me do just that. I drop it in my user root, and can spin it up with one line from any directory - turning that directory into a web server instantly.

from http.server import HTTPServer, SimpleHTTPRequestHandler, test
import sys

class CORSRequestHandler (SimpleHTTPRequestHandler):
  def end_headers (self):
    self.send_header('Access-Control-Allow-Origin', '*')
        
    self.send_header('Cache-Control', 'no-cache, no-store, must-revalidate')
    self.send_header('Pragma', 'no-cache')
    self.send_header('Expires', '0')
    
    SimpleHTTPRequestHandler.end_headers(self)

if __name__ == '__main__':
  test(CORSRequestHandler, HTTPServer, port=int(sys.argv[1]) if len(sys.argv) > 1 else 8000)
python3 ~/server.py
Back to Notes