Version: Nov 29, 2024

Notes: Running on MacOS, getting Python 3 on Homebrew, then creating a script to launch a local server that doesn't cache.

Setting up a local server

1. Get Homebrew

2. Get Python 3

1. Verify Python Installation

2. Update PATH (Optional)

If Python 3 is not recognized after installation, ensure Homebrew’s Python path is added to your PATH variable.

3. Install Pip (Optional)

4. Test Python Installation

3. Creating the server script

import http.server
import socketserver
import threading

class NoCacheHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
    def end_headers(self):
        # Add Cache-Control headers to prevent caching
        self.send_header("Cache-Control", "no-cache, no-store, must-revalidate")
        self.send_header("Pragma", "no-cache")
        self.send_header("Expires", "0")
        super().end_headers()

# Define the port
PORT = 8000

def start_server():
    try:
        with socketserver.TCPServer(("", PORT), NoCacheHTTPRequestHandler) as httpd:
            print(f"Serving at http://localhost:{PORT}")
            httpd.serve_forever()
    except KeyboardInterrupt:
        print("\nServer interrupted. Shutting down gracefully...")
        httpd.server_close()

if __name__ == "__main__":
    # Run the server in a thread for improved responsiveness
    server_thread = threading.Thread(target=start_server, daemon=True)
    server_thread.start()

    try:
        while True:
            pass  # Keep the main thread alive
    except KeyboardInterrupt:
        print("\nExiting...")