• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1
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.. note::
10   The :mod:`SimpleHTTPServer` module has been merged into :mod:`http.server` in
11   Python 3.  The :term:`2to3` tool will automatically adapt imports when
12   converting your sources to Python 3.
13
14
15The :mod:`SimpleHTTPServer` module defines a single class,
16:class:`SimpleHTTPRequestHandler`, which is interface-compatible with
17:class:`BaseHTTPServer.BaseHTTPRequestHandler`.
18
19The :mod:`SimpleHTTPServer` module defines the following class:
20
21
22.. class:: SimpleHTTPRequestHandler(request, client_address, server)
23
24   This class serves files from the current directory and below, directly
25   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
31   The following are defined as class-level attributes of
32   :class:`SimpleHTTPRequestHandler`:
33
34
35   .. attribute:: server_version
36
37   This will be ``"SimpleHTTP/" + __version__``, where ``__version__`` is
38   defined at the module level.
39
40
41   .. attribute:: extensions_map
42
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.
47
48   The :class:`SimpleHTTPRequestHandler` class defines the following methods:
49
50
51   .. method:: do_HEAD()
52
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.
56
57
58   .. method:: do_GET()
59
60      The request is mapped to a local file by interpreting the request as a
61      path relative to the current working directory.
62
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.
69
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.
75
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.
79
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.
83
84      The :func:`test` function in the :mod:`SimpleHTTPServer` module is an
85      example which creates a server using the :class:`SimpleHTTPRequestHandler`
86      as the Handler.
87
88      .. versionadded:: 2.5
89         The ``'Last-Modified'`` header.
90
91
92The :mod:`SimpleHTTPServer` module can be used in the following manner in order
93to set up a very basic web server serving files relative to the current
94directory. ::
95
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
108The :mod:`SimpleHTTPServer` module can also be invoked directly using the
109:option:`-m` switch of the interpreter with a ``port number`` argument.
110Similar to the previous example, this serves the files relative to the
111current directory. ::
112
113   python -m SimpleHTTPServer 8000
114
115.. seealso::
116
117   Module :mod:`BaseHTTPServer`
118      Base class implementation for Web server and request handler.
119
120