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