blob: ecb93408fa04d122495ab45c3450dc4e9bad8838 [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
Georg Brandl8de91192008-05-26 15:01:48 +00009.. note::
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.
13
Georg Brandl8ec7f652007-08-15 14:28:01 +000014
Skip Montanaroef1e58b2008-04-27 20:53:57 +000015The :mod:`SimpleHTTPServer` module defines a single class,
16:class:`SimpleHTTPRequestHandler`, which is interface-compatible with
17:class:`BaseHTTPServer.BaseHTTPRequestHandler`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000018
19The :mod:`SimpleHTTPServer` module defines the following class:
20
21
22.. class:: SimpleHTTPRequestHandler(request, client_address, server)
23
Skip Montanaroef1e58b2008-04-27 20:53:57 +000024 This class serves files from the current directory and below, directly
Georg Brandl8ec7f652007-08-15 14:28:01 +000025 mapping the directory structure to HTTP requests.
26
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.
30
Skip Montanaroef1e58b2008-04-27 20:53:57 +000031 The following are defined as class-level attributes of
32 :class:`SimpleHTTPRequestHandler`:
Georg Brandl8ec7f652007-08-15 14:28:01 +000033
34
Benjamin Petersonc7b05922008-04-25 01:29:10 +000035 .. attribute:: server_version
Georg Brandl8ec7f652007-08-15 14:28:01 +000036
Skip Montanaroef1e58b2008-04-27 20:53:57 +000037 This will be ``"SimpleHTTP/" + __version__``, where ``__version__`` is
38 defined at the module level.
Georg Brandl8ec7f652007-08-15 14:28:01 +000039
40
Benjamin Petersonc7b05922008-04-25 01:29:10 +000041 .. attribute:: extensions_map
Georg Brandl8ec7f652007-08-15 14:28:01 +000042
Skip Montanaroef1e58b2008-04-27 20:53:57 +000043 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.
Georg Brandl8ec7f652007-08-15 14:28:01 +000047
Skip Montanaroef1e58b2008-04-27 20:53:57 +000048 The :class:`SimpleHTTPRequestHandler` class defines the following methods:
Georg Brandl8ec7f652007-08-15 14:28:01 +000049
50
Benjamin Petersonc7b05922008-04-25 01:29:10 +000051 .. method:: do_HEAD()
Georg Brandl8ec7f652007-08-15 14:28:01 +000052
Benjamin Petersonc7b05922008-04-25 01:29:10 +000053 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.
Georg Brandl8ec7f652007-08-15 14:28:01 +000056
57
Benjamin Petersonc7b05922008-04-25 01:29:10 +000058 .. method:: do_GET()
Georg Brandl8ec7f652007-08-15 14:28:01 +000059
Benjamin Petersonc7b05922008-04-25 01:29:10 +000060 The request is mapped to a local file by interpreting the request as a
61 path relative to the current working directory.
Georg Brandl8ec7f652007-08-15 14:28:01 +000062
Benjamin Petersonc7b05922008-04-25 01:29:10 +000063 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.
Georg Brandl8ec7f652007-08-15 14:28:01 +000069
Benjamin Petersonc7b05922008-04-25 01:29:10 +000070 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.
Georg Brandl8ec7f652007-08-15 14:28:01 +000075
Benjamin Petersonc7b05922008-04-25 01:29:10 +000076 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.
Georg Brandl8ec7f652007-08-15 14:28:01 +000079
Benjamin Petersonc7b05922008-04-25 01:29:10 +000080 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.
Georg Brandl8ec7f652007-08-15 14:28:01 +000083
Senthil Kumarane55a2052010-06-16 14:55:31 +000084 The :func:`test` function in the :mod:`SimpleHTTPServer` module is an
Senthil Kumarana3e3e362010-06-16 17:37:32 +000085 example which creates a server using the
86 :class:`SimpleHTTPRequestHandler` as the Handler.
Georg Brandl8ec7f652007-08-15 14:28:01 +000087
Benjamin Petersonc7b05922008-04-25 01:29:10 +000088 .. versionadded:: 2.5
89 The ``'Last-Modified'`` header.
Georg Brandl8ec7f652007-08-15 14:28:01 +000090
91
Senthil Kumarane55a2052010-06-16 14:55:31 +000092The :mod:`SimpleHTTPServer` module can be used the following manner in order to
Senthil Kumarana3e3e362010-06-16 17:37:32 +000093set up a very basic web server serving files relative to the current
94directory.::
Senthil Kumarane55a2052010-06-16 14:55:31 +000095
96 import SimpleHTTPServer
97 import SocketServer
98
99 PORT = 8000
100
101 Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
102
103 httpd = SocketServer.TCPServer(("", PORT), Handler)
104
105 print "serving at port", PORT
106 httpd.serve_forever()
107
Senthil Kumarana3e3e362010-06-16 17:37:32 +0000108:mod:`SimpleHTTPServer` module can also be invoked directly using the ``-m``
109switch of interpreter with a ``port number`` argument. Similar to previous
110example, this serves the files relative to the current directory.::
Senthil Kumarane55a2052010-06-16 14:55:31 +0000111
112 python -m SimpleHTTPServer 8000
113
Georg Brandl8ec7f652007-08-15 14:28:01 +0000114.. seealso::
115
116 Module :mod:`BaseHTTPServer`
117 Base class implementation for Web server and request handler.
118