Http.server: custom not found (404.html) page handling

I’ve been using http module via CLI for serving static website (local development). For handling HTTP 404, I see our(python) http server has standard template. However, on my ssg, I have custom not found page - 404.html and if that exists I wanted that to be rendered implicitly. This behaviour can be achieved in e.g. serve.

setup:

http(in CPython, stdlib) - standard template, even though I have 404.html:

serve (in NodeJS, by vercel) - it renders my 404.html:

I wanted to have community’s thoughts on this, and I think it is good enhancement.

“If 404.html exists(alongside index.html), render this on HTTP 404 otherwise fallback to current template”.

Thank you.

P.S I couldn’t find any relevant discussions on this topic.

The http.server module, particularly in its CLI mode, is designed to be simple and easy; it won’t have all the tools you might want.

IMPORTANT CAVEAT: This module is not recommended for production, as it performs only the bare minimum of security checks. I don’t know how relevant this is to you but just be aware, this is neither high performance nor hardened.

What you may need to do is create a small module that imports http.server, subclasses BaseHTTPRequestHandler, and then creates a server and serves forever (see example code in the module docs). In your subclass, you can then override the send_error method; if it’s given any response code other than 404, call the superclass, but you can then give whatever response you want for the 404.

This should be fairly straight-forward (maybe half a page of code), but it won’t be as easy as the command line super-simple server.

Yes, I’m aware - that’s why I put “local development” phrase.

On creating module - that is what I found too from web search (e.g. stackoverflow), and I did those kind of stuff (even before this case).

I though that it(http 404) must be explicitly handled for custom pages (like you sad subclassing, overriding …) - but when I saw serve then I had this though(s).

That’s why opened discussion, maybe that enhancement might be useful.