Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame^] | 1 | |
| 2 | :mod:`SimpleHTTPServer` --- Simple HTTP request handler |
| 3 | ======================================================= |
| 4 | |
| 5 | .. module:: SimpleHTTPServer |
| 6 | :synopsis: This module provides a basic request handler for HTTP servers. |
| 7 | .. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il> |
| 8 | |
| 9 | |
| 10 | The :mod:`SimpleHTTPServer` module defines a request-handler class, |
| 11 | interface-compatible with :class:`BaseHTTPServer.BaseHTTPRequestHandler`, that |
| 12 | serves files only from a base directory. |
| 13 | |
| 14 | The :mod:`SimpleHTTPServer` module defines the following class: |
| 15 | |
| 16 | |
| 17 | .. class:: SimpleHTTPRequestHandler(request, client_address, server) |
| 18 | |
| 19 | This class is used to serve files from the current directory and below, directly |
| 20 | mapping the directory structure to HTTP requests. |
| 21 | |
| 22 | A lot of the work, such as parsing the request, is done by the base class |
| 23 | :class:`BaseHTTPServer.BaseHTTPRequestHandler`. This class implements the |
| 24 | :func:`do_GET` and :func:`do_HEAD` functions. |
| 25 | |
| 26 | The :class:`SimpleHTTPRequestHandler` defines the following member variables: |
| 27 | |
| 28 | |
| 29 | .. attribute:: SimpleHTTPRequestHandler.server_version |
| 30 | |
| 31 | This will be ``"SimpleHTTP/" + __version__``, where ``__version__`` is defined |
| 32 | in the module. |
| 33 | |
| 34 | |
| 35 | .. attribute:: SimpleHTTPRequestHandler.extensions_map |
| 36 | |
| 37 | A dictionary mapping suffixes into MIME types. The default is signified by an |
| 38 | empty string, and is considered to be ``application/octet-stream``. The mapping |
| 39 | is used case-insensitively, and so should contain only lower-cased keys. |
| 40 | |
| 41 | The :class:`SimpleHTTPRequestHandler` defines the following methods: |
| 42 | |
| 43 | |
| 44 | .. method:: SimpleHTTPRequestHandler.do_HEAD() |
| 45 | |
| 46 | This method serves the ``'HEAD'`` request type: it sends the headers it would |
| 47 | send for the equivalent ``GET`` request. See the :meth:`do_GET` method for a |
| 48 | more complete explanation of the possible headers. |
| 49 | |
| 50 | |
| 51 | .. method:: SimpleHTTPRequestHandler.do_GET() |
| 52 | |
| 53 | The request is mapped to a local file by interpreting the request as a path |
| 54 | relative to the current working directory. |
| 55 | |
| 56 | If the request was mapped to a directory, the directory is checked for a file |
| 57 | named ``index.html`` or ``index.htm`` (in that order). If found, the file's |
| 58 | contents are returned; otherwise a directory listing is generated by calling the |
| 59 | :meth:`list_directory` method. This method uses :func:`os.listdir` to scan the |
| 60 | directory, and returns a ``404`` error response if the :func:`listdir` fails. |
| 61 | |
| 62 | If the request was mapped to a file, it is opened and the contents are returned. |
| 63 | Any :exc:`IOError` exception in opening the requested file is mapped to a |
| 64 | ``404``, ``'File not found'`` error. Otherwise, the content type is guessed by |
| 65 | calling the :meth:`guess_type` method, which in turn uses the *extensions_map* |
| 66 | variable. |
| 67 | |
| 68 | A ``'Content-type:'`` header with the guessed content type is output, followed |
| 69 | by a ``'Content-Length:'`` header with the file's size and a |
| 70 | ``'Last-Modified:'`` header with the file's modification time. |
| 71 | |
| 72 | Then follows a blank line signifying the end of the headers, and then the |
| 73 | contents of the file are output. If the file's MIME type starts with ``text/`` |
| 74 | the file is opened in text mode; otherwise binary mode is used. |
| 75 | |
| 76 | For example usage, see the implementation of the :func:`test` function. |
| 77 | |
| 78 | .. versionadded:: 2.5 |
| 79 | The ``'Last-Modified'`` header. |
| 80 | |
| 81 | |
| 82 | .. seealso:: |
| 83 | |
| 84 | Module :mod:`BaseHTTPServer` |
| 85 | Base class implementation for Web server and request handler. |
| 86 | |