blob: 3c2d72b88768f60ee4a1628a3d24765c79693acd [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +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
Skip Montanaroef1e58b2008-04-27 20:53:57 +000010The :mod:`SimpleHTTPServer` module defines a single class,
11:class:`SimpleHTTPRequestHandler`, which is interface-compatible with
12:class:`BaseHTTPServer.BaseHTTPRequestHandler`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000013
14The :mod:`SimpleHTTPServer` module defines the following class:
15
16
17.. class:: SimpleHTTPRequestHandler(request, client_address, server)
18
Skip Montanaroef1e58b2008-04-27 20:53:57 +000019 This class serves files from the current directory and below, directly
Georg Brandl8ec7f652007-08-15 14:28:01 +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
Skip Montanaroef1e58b2008-04-27 20:53:57 +000026 The following are defined as class-level attributes of
27 :class:`SimpleHTTPRequestHandler`:
Georg Brandl8ec7f652007-08-15 14:28:01 +000028
29
Benjamin Petersonc7b05922008-04-25 01:29:10 +000030 .. attribute:: server_version
Georg Brandl8ec7f652007-08-15 14:28:01 +000031
Skip Montanaroef1e58b2008-04-27 20:53:57 +000032 This will be ``"SimpleHTTP/" + __version__``, where ``__version__`` is
33 defined at the module level.
Georg Brandl8ec7f652007-08-15 14:28:01 +000034
35
Benjamin Petersonc7b05922008-04-25 01:29:10 +000036 .. attribute:: extensions_map
Georg Brandl8ec7f652007-08-15 14:28:01 +000037
Skip Montanaroef1e58b2008-04-27 20:53:57 +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 Brandl8ec7f652007-08-15 14:28:01 +000042
Skip Montanaroef1e58b2008-04-27 20:53:57 +000043 The :class:`SimpleHTTPRequestHandler` class defines the following methods:
Georg Brandl8ec7f652007-08-15 14:28:01 +000044
45
Benjamin Petersonc7b05922008-04-25 01:29:10 +000046 .. method:: do_HEAD()
Georg Brandl8ec7f652007-08-15 14:28:01 +000047
Benjamin Petersonc7b05922008-04-25 01:29:10 +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 Brandl8ec7f652007-08-15 14:28:01 +000051
52
Benjamin Petersonc7b05922008-04-25 01:29:10 +000053 .. method:: do_GET()
Georg Brandl8ec7f652007-08-15 14:28:01 +000054
Benjamin Petersonc7b05922008-04-25 01:29:10 +000055 The request is mapped to a local file by interpreting the request as a
56 path relative to the current working directory.
Georg Brandl8ec7f652007-08-15 14:28:01 +000057
Benjamin Petersonc7b05922008-04-25 01:29:10 +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 Brandl8ec7f652007-08-15 14:28:01 +000064
Benjamin Petersonc7b05922008-04-25 01:29:10 +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 Brandl8ec7f652007-08-15 14:28:01 +000070
Benjamin Petersonc7b05922008-04-25 01:29:10 +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 Brandl8ec7f652007-08-15 14:28:01 +000074
Benjamin Petersonc7b05922008-04-25 01:29:10 +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 Brandl8ec7f652007-08-15 14:28:01 +000078
Benjamin Petersonc7b05922008-04-25 01:29:10 +000079 For example usage, see the implementation of the :func:`test` function.
Georg Brandl8ec7f652007-08-15 14:28:01 +000080
Benjamin Petersonc7b05922008-04-25 01:29:10 +000081 .. versionadded:: 2.5
82 The ``'Last-Modified'`` header.
Georg Brandl8ec7f652007-08-15 14:28:01 +000083
84
85.. seealso::
86
87 Module :mod:`BaseHTTPServer`
88 Base class implementation for Web server and request handler.
89