blob: 8d04e3faaf90a2c6f1beb55f43081cfe8178574d [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
10The :mod:`SimpleHTTPServer` module defines a request-handler class,
11interface-compatible with :class:`BaseHTTPServer.BaseHTTPRequestHandler`, that
12serves files only from a base directory.
13
14The :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 Petersonc7b05922008-04-25 01:29:10 +000026 The :class:`SimpleHTTPRequestHandler` defines the following member variables:
Georg Brandl8ec7f652007-08-15 14:28:01 +000027
28
Benjamin Petersonc7b05922008-04-25 01:29:10 +000029 .. attribute:: server_version
Georg Brandl8ec7f652007-08-15 14:28:01 +000030
Benjamin Petersonc7b05922008-04-25 01:29:10 +000031 This will be ``"SimpleHTTP/" + __version__``, where ``__version__`` is
32 defined in the module.
Georg Brandl8ec7f652007-08-15 14:28:01 +000033
34
Benjamin Petersonc7b05922008-04-25 01:29:10 +000035 .. attribute:: extensions_map
Georg Brandl8ec7f652007-08-15 14:28:01 +000036
Benjamin Petersonc7b05922008-04-25 01:29:10 +000037 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 Brandl8ec7f652007-08-15 14:28:01 +000041
Benjamin Petersonc7b05922008-04-25 01:29:10 +000042 The :class:`SimpleHTTPRequestHandler` defines the following methods:
Georg Brandl8ec7f652007-08-15 14:28:01 +000043
44
Benjamin Petersonc7b05922008-04-25 01:29:10 +000045 .. method:: do_HEAD()
Georg Brandl8ec7f652007-08-15 14:28:01 +000046
Benjamin Petersonc7b05922008-04-25 01:29:10 +000047 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 Brandl8ec7f652007-08-15 14:28:01 +000050
51
Benjamin Petersonc7b05922008-04-25 01:29:10 +000052 .. method:: do_GET()
Georg Brandl8ec7f652007-08-15 14:28:01 +000053
Benjamin Petersonc7b05922008-04-25 01:29:10 +000054 The request is mapped to a local file by interpreting the request as a
55 path relative to the current working directory.
Georg Brandl8ec7f652007-08-15 14:28:01 +000056
Benjamin Petersonc7b05922008-04-25 01:29:10 +000057 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 Brandl8ec7f652007-08-15 14:28:01 +000063
Benjamin Petersonc7b05922008-04-25 01:29:10 +000064 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 Brandl8ec7f652007-08-15 14:28:01 +000069
Benjamin Petersonc7b05922008-04-25 01:29:10 +000070 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 Brandl8ec7f652007-08-15 14:28:01 +000073
Benjamin Petersonc7b05922008-04-25 01:29:10 +000074 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 Brandl8ec7f652007-08-15 14:28:01 +000077
Benjamin Petersonc7b05922008-04-25 01:29:10 +000078 For example usage, see the implementation of the :func:`test` function.
Georg Brandl8ec7f652007-08-15 14:28:01 +000079
Benjamin Petersonc7b05922008-04-25 01:29:10 +000080 .. versionadded:: 2.5
81 The ``'Last-Modified'`` header.
Georg Brandl8ec7f652007-08-15 14:28:01 +000082
83
84.. seealso::
85
86 Module :mod:`BaseHTTPServer`
87 Base class implementation for Web server and request handler.
88