2 :mod:`SimpleHTTPServer` --- Simple HTTP request handler
3 =======================================================
5 .. module:: SimpleHTTPServer
6 :synopsis: This module provides a basic request handler for HTTP servers.
7 .. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
10 The :mod:`SimpleHTTPServer` module has been merged into :mod:`http.server` in
11 Python 3.0. The :term:`2to3` tool will automatically adapt imports when
12 converting your sources to 3.0.
15 The :mod:`SimpleHTTPServer` module defines a single class,
16 :class:`SimpleHTTPRequestHandler`, which is interface-compatible with
17 :class:`BaseHTTPServer.BaseHTTPRequestHandler`.
19 The :mod:`SimpleHTTPServer` module defines the following class:
22 .. class:: SimpleHTTPRequestHandler(request, client_address, server)
24 This class serves files from the current directory and below, directly
25 mapping the directory structure to HTTP requests.
27 A lot of the work, such as parsing the request, is done by the base class
28 :class:`BaseHTTPServer.BaseHTTPRequestHandler`. This class implements the
29 :func:`do_GET` and :func:`do_HEAD` functions.
31 The following are defined as class-level attributes of
32 :class:`SimpleHTTPRequestHandler`:
35 .. attribute:: server_version
37 This will be ``"SimpleHTTP/" + __version__``, where ``__version__`` is
38 defined at the module level.
41 .. attribute:: extensions_map
43 A dictionary mapping suffixes into MIME types. The default is
44 signified by an empty string, and is considered to be
45 ``application/octet-stream``. The mapping is used case-insensitively,
46 and so should contain only lower-cased keys.
48 The :class:`SimpleHTTPRequestHandler` class defines the following methods:
53 This method serves the ``'HEAD'`` request type: it sends the headers it
54 would send for the equivalent ``GET`` request. See the :meth:`do_GET`
55 method for a more complete explanation of the possible headers.
60 The request is mapped to a local file by interpreting the request as a
61 path relative to the current working directory.
63 If the request was mapped to a directory, the directory is checked for a
64 file named ``index.html`` or ``index.htm`` (in that order). If found, the
65 file's contents are returned; otherwise a directory listing is generated
66 by calling the :meth:`list_directory` method. This method uses
67 :func:`os.listdir` to scan the directory, and returns a ``404`` error
68 response if the :func:`listdir` fails.
70 If the request was mapped to a file, it is opened and the contents are
71 returned. Any :exc:`IOError` exception in opening the requested file is
72 mapped to a ``404``, ``'File not found'`` error. Otherwise, the content
73 type is guessed by calling the :meth:`guess_type` method, which in turn
74 uses the *extensions_map* variable.
76 A ``'Content-type:'`` header with the guessed content type is output,
77 followed by a ``'Content-Length:'`` header with the file's size and a
78 ``'Last-Modified:'`` header with the file's modification time.
80 Then follows a blank line signifying the end of the headers, and then the
81 contents of the file are output. If the file's MIME type starts with
82 ``text/`` the file is opened in text mode; otherwise binary mode is used.
84 For example usage, see the implementation of the :func:`test` function.
87 The ``'Last-Modified'`` header.
92 Module :mod:`BaseHTTPServer`
93 Base class implementation for Web server and request handler.