From the http.server docs:
CGIHTTPRequestHandlercan be enabled in the command line by passing
the--cgioption:
$ python3 -m http.server --bind localhost --cgi 8000
Put your script into cgi_directories:
This defaults to
['/cgi-bin', '/htbin']and describes directories to treat as containing CGI scripts.
Open in the browser:
$ python -mwebbrowser http://localhost:8000/cgi-bin/hello.py
where hello.py:
#!/usr/bin/env python3
print("Content-Type: text/html\n")
print("<!doctype html><title>Hello</title><h2>hello world</h2>")
I had to make it executable on POSIX: chmod +x cgi-bin/hello.py.