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