blob: 7d996810ec7acea0c19963ee14f78c703401adb7 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
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
Christian Heimes81ee3ef2008-05-04 22:42:01 +000010The :mod:`SimpleHTTPServer` module defines a single class,
11:class:`SimpleHTTPRequestHandler`, which is interface-compatible with
12:class:`BaseHTTPServer.BaseHTTPRequestHandler`.
Georg Brandl116aa622007-08-15 14:28:22 +000013
14The :mod:`SimpleHTTPServer` module defines the following class:
15
16
17.. class:: SimpleHTTPRequestHandler(request, client_address, server)
18
Christian Heimes81ee3ef2008-05-04 22:42:01 +000019 This class serves files from the current directory and below, directly
Georg Brandl116aa622007-08-15 14:28:22 +000020 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
Christian Heimes81ee3ef2008-05-04 22:42:01 +000026 The following are defined as class-level attributes of
27 :class:`SimpleHTTPRequestHandler`:
Georg Brandl116aa622007-08-15 14:28:22 +000028
29
Benjamin Petersone41251e2008-04-25 01:59:09 +000030 .. attribute:: server_version
Georg Brandl116aa622007-08-15 14:28:22 +000031
Christian Heimes81ee3ef2008-05-04 22:42:01 +000032 This will be ``"SimpleHTTP/" + __version__``, where ``__version__`` is
33 defined at the module level.
Georg Brandl116aa622007-08-15 14:28:22 +000034
35
Benjamin Petersone41251e2008-04-25 01:59:09 +000036 .. attribute:: extensions_map
Georg Brandl116aa622007-08-15 14:28:22 +000037
Christian Heimes81ee3ef2008-05-04 22:42:01 +000038 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 Brandl116aa622007-08-15 14:28:22 +000042
Christian Heimes81ee3ef2008-05-04 22:42:01 +000043 The :class:`SimpleHTTPRequestHandler` class defines the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +000044
45
Benjamin Petersone41251e2008-04-25 01:59:09 +000046 .. method:: do_HEAD()
Georg Brandl116aa622007-08-15 14:28:22 +000047
Benjamin Petersone41251e2008-04-25 01:59:09 +000048 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 Brandl116aa622007-08-15 14:28:22 +000051
52
Benjamin Petersone41251e2008-04-25 01:59:09 +000053 .. method:: do_GET()
Georg Brandl116aa622007-08-15 14:28:22 +000054
Benjamin Petersone41251e2008-04-25 01:59:09 +000055 The request is mapped to a local file by interpreting the request as a
56 path relative to the current working directory.
Georg Brandl116aa622007-08-15 14:28:22 +000057
Benjamin Petersone41251e2008-04-25 01:59:09 +000058 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 Brandl116aa622007-08-15 14:28:22 +000064
Benjamin Petersone41251e2008-04-25 01:59:09 +000065 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 Brandl116aa622007-08-15 14:28:22 +000070
Benjamin Petersone41251e2008-04-25 01:59:09 +000071 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 Brandl116aa622007-08-15 14:28:22 +000074
Benjamin Petersone41251e2008-04-25 01:59:09 +000075 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 Brandl116aa622007-08-15 14:28:22 +000078
Benjamin Petersone41251e2008-04-25 01:59:09 +000079 For example usage, see the implementation of the :func:`test` function.
Georg Brandl116aa622007-08-15 14:28:22 +000080
Georg Brandl116aa622007-08-15 14:28:22 +000081
82.. seealso::
83
84 Module :mod:`BaseHTTPServer`
85 Base class implementation for Web server and request handler.
86