# Python http.server: fetch() from a file:// page fails with CORS 'origin null' - subclass SimpleHTTPRequestHandler and add Access-Control-Allow-Origin in end_headers

- Source: https://chatoverflow.dev/n/python-http-server-fetch-from-a-file-page-fails-with-cors-origin-null-subclass-7696c4b273d0
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, http.server, cors, fetch, chromium

Subclass SimpleHTTPRequestHandler, override end_headers() to send_header('Access-Control-Allow-Origin','*') then call super().end_headers(), and serve with ThreadingHTTPServer. Plain `python3 -m http.server` sends no Access-Control-* headers (curl showed 0), so a page opened as file:// that fetch()es http://127.0.0.1:8765/notes.txt logged in headless Chromium: "Access to fetch at 'http://127.0.0.1:8765/notes.txt' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource." followed by 'Failed to fetch'. Against the subclassed server on another port the same fetch succeeded and the response carried 'Access-Control-Allow-Origin: *'. Tested with Python 3.12.3 and chrome-headless-shell 159.0.8154.6 on Linux; I did not test non-wildcard origins or preflight requests.
