Search notes:

Python standard library: http.server

$ python3 -m http.server --help
usage: server.py [-h] [--cgi] [--bind ADDRESS] [--directory DIRECTORY] [port]

positional arguments:
  port                  Specify alternate port [default: 8000]

optional arguments:
  -h, --help            show this help message and exit
  --cgi                 Run as CGI Server
  --bind ADDRESS, -b ADDRESS
                        Specify alternate bind address [default: all interfaces]
  --directory DIRECTORY, -d DIRECTORY
                        Specify alternative directory [default:current directory]

Adding headers to HTTP response

In order to use the SharedArrayBuffer object, the two HTTP response headers Cross-Origin-Opener-Policy and Cross-Origin-Embedder-Policy must be set to same-origin and require-corp.
If these header are not set or set to a different value, the following simple HTML page alerts the user that SharedArrayBuffer is not supported:
<script>
   if (typeof(SharedArrayBuffer) === 'undefined') {
      alert('SharedArrayBuffer is not supported');
   }
   else {
      alert('SharedArrayBuffer IS supported');
   }
</script>
The following script sets the two required headers so that the browser does support SharedArrayBuffer:
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer

class serveWithHeaders(SimpleHTTPRequestHandler):

    def end_headers(self):
        self.addHeaders()

        SimpleHTTPRequestHandler.end_headers(self)

    def addHeaders(self):
        self.send_header('Cross-Origin-Opener-Policy',   'same-origin' )
        self.send_header('Cross-Origin-Embedder-Policy', 'require-corp')

httpd = ThreadingHTTPServer( ('', 8000), serveWithHeaders)

httpd.serve_forever()
See also Python Webserver for CORS compatible HTTP requests

BaseHTTPRequestHandler

Interesting functions and properties

Not production ready

As per the documentation, http.server implements only basic security checks and is therefore not production ready.
Some possible problems are addressed in this Stack Exchange/Information Security question.

Some notes on the code

The constructor of BaseServer takes
The constructor of (the derived class) TCPServer additionally takes the
The (derived classes) http.HTTPServer and http.ThreadingHTTPServer do not have an explicit constructor.
BaseRequestHandler is the base class for objects that handle requests.
This object is instantiated in the method BaseServer.finish_request.
BaseServer.serve_forever() handles one request at a time until shutdown.
serve_forever() calls BaseServer._handle_request_noblock() to handle a request.
BaseServer._handle_request_noblock()
BaseServer.process_request() calls
BaseServer.finish_request()
StreamRequestHandler does not override handle().
StreamRequestHandler.setup() creates the members rfile and wfile.
class BaseHTTPRequestHandler
BaseHTTPRequestHandler.handle() calls handle_one_request() until self.close_connection is True.
BaseHTTPRequestHandler.handle_one_request() (which normally does not need to be overridden) handles one HTTP request.
This method
BaseHTTPRequestHandler.parse_request()
The constructor of BaseRequestHandler
The constructor of TCPServer
TCPServer.get_request() calls self.socket.accept() which returns request (a socket object) and client_address.

TODO

Is Python 2 SimpleHTTPServer merged into http.server in Python 3?

See also

Webservers
standard library (especially http.client)

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1758198569, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Python/standard-library/http/server/index(189): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78