11.17 SimpleHTTPServer -- Simple HTTP request handler
Keywords: lib

# 11.17 `SimpleHTTPServer` -- Simple HTTP request handler

The `SimpleHTTPServer` module defines a request-handler class, interface compatible with `BaseHTTPServer.BaseHTTPRequestHandler` which serves files only from a base directory.

The `SimpleHTTPServer` module defines the following class:

class `SimpleHTTPRequestHandler`(request, client_address, server)
  This class is used, to serve files from current directory and below, directly mapping the directory structure to HTTP requests.

  A lot of the work is done by the base class `BaseHTTPServer.BaseHTTPRequestHandler`, such as parsing the request. This class implements the `do_GET()` and `do_HEAD()` functions.

The `SimpleHTTPRequestHandler` defines the following member variables:

`server_version`
  This will be `"SimpleHTTP/" + __version__`, where `__version__` is defined in the module.

`extensions_map`
  A dictionary mapping suffixes into MIME types. Default is signified by an empty string, and is considered to be `text/plain`. The mapping is used case-insensitively, and so should contain only lower-cased keys.

The `SimpleHTTPRequestHandler` defines the following methods:

`do_HEAD`()
  This method serves the `'HEAD'` request type: it sends the headers it would send for the equivalent `GET` request. See the `do_GET()` method for more complete explanation of the possible headers.

`do_GET`()
  The request is mapped to a local file by interpreting the request as a path relative to the current working directory.

  If the request was mapped to a directory, a `403` respond is output, followed by the explanation `'Directory listing not supported'`. Any `IOError` exception in opening the requested file, is mapped to a `404`, `'File not found'` error. Otherwise, the content type is guessed using the extensions_map variable.

  A `'Content-type:'` with the guessed content type is output, and then a blank line, signifying end of headers, and then the contents of the file. The file is always opened in binary mode.

  For example usage, see the implementation of the `test()` function.

See Also:

Module `BaseHTTPServer`:
  Base class implementation for Web server and request handler.