Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 8de9119 | 2008-05-26 15:01:48 +0000 | [diff] [blame] | 9 | .. note:: |
| 10 | The :mod:`SimpleHTTPServer` module has been merged into :mod:`http.server` in |
Ezio Melotti | 510ff54 | 2012-05-03 19:21:40 +0300 | [diff] [blame] | 11 | Python 3. The :term:`2to3` tool will automatically adapt imports when |
| 12 | converting your sources to Python 3. |
Georg Brandl | 8de9119 | 2008-05-26 15:01:48 +0000 | [diff] [blame] | 13 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 14 | |
Skip Montanaro | ef1e58b | 2008-04-27 20:53:57 +0000 | [diff] [blame] | 15 | The :mod:`SimpleHTTPServer` module defines a single class, |
| 16 | :class:`SimpleHTTPRequestHandler`, which is interface-compatible with |
| 17 | :class:`BaseHTTPServer.BaseHTTPRequestHandler`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 18 | |
| 19 | The :mod:`SimpleHTTPServer` module defines the following class: |
| 20 | |
| 21 | |
| 22 | .. class:: SimpleHTTPRequestHandler(request, client_address, server) |
| 23 | |
Skip Montanaro | ef1e58b | 2008-04-27 20:53:57 +0000 | [diff] [blame] | 24 | This class serves files from the current directory and below, directly |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 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 | |
Skip Montanaro | ef1e58b | 2008-04-27 20:53:57 +0000 | [diff] [blame] | 31 | The following are defined as class-level attributes of |
| 32 | :class:`SimpleHTTPRequestHandler`: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 33 | |
| 34 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 35 | .. attribute:: server_version |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 36 | |
Skip Montanaro | ef1e58b | 2008-04-27 20:53:57 +0000 | [diff] [blame] | 37 | This will be ``"SimpleHTTP/" + __version__``, where ``__version__`` is |
| 38 | defined at the module level. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 39 | |
| 40 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 41 | .. attribute:: extensions_map |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 42 | |
Skip Montanaro | ef1e58b | 2008-04-27 20:53:57 +0000 | [diff] [blame] | 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. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 47 | |
Skip Montanaro | ef1e58b | 2008-04-27 20:53:57 +0000 | [diff] [blame] | 48 | The :class:`SimpleHTTPRequestHandler` class defines the following methods: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 49 | |
| 50 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 51 | .. method:: do_HEAD() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 52 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 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. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 56 | |
| 57 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 58 | .. method:: do_GET() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 59 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 60 | The request is mapped to a local file by interpreting the request as a |
| 61 | path relative to the current working directory. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 62 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 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. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 69 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 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. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 75 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 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. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 79 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 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. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 83 | |
Senthil Kumaran | e55a205 | 2010-06-16 14:55:31 +0000 | [diff] [blame] | 84 | The :func:`test` function in the :mod:`SimpleHTTPServer` module is an |
Georg Brandl | 0ada39c | 2010-07-02 07:33:50 +0000 | [diff] [blame] | 85 | example which creates a server using the :class:`SimpleHTTPRequestHandler` |
| 86 | as the Handler. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 87 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 88 | .. versionadded:: 2.5 |
| 89 | The ``'Last-Modified'`` header. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 90 | |
| 91 | |
Georg Brandl | 0ada39c | 2010-07-02 07:33:50 +0000 | [diff] [blame] | 92 | The :mod:`SimpleHTTPServer` module can be used in the following manner in order |
| 93 | to set up a very basic web server serving files relative to the current |
| 94 | directory. :: |
Senthil Kumaran | e55a205 | 2010-06-16 14:55:31 +0000 | [diff] [blame] | 95 | |
Georg Brandl | 0ada39c | 2010-07-02 07:33:50 +0000 | [diff] [blame] | 96 | import SimpleHTTPServer |
| 97 | import SocketServer |
Senthil Kumaran | e55a205 | 2010-06-16 14:55:31 +0000 | [diff] [blame] | 98 | |
Georg Brandl | 0ada39c | 2010-07-02 07:33:50 +0000 | [diff] [blame] | 99 | PORT = 8000 |
Senthil Kumaran | e55a205 | 2010-06-16 14:55:31 +0000 | [diff] [blame] | 100 | |
Georg Brandl | 0ada39c | 2010-07-02 07:33:50 +0000 | [diff] [blame] | 101 | Handler = SimpleHTTPServer.SimpleHTTPRequestHandler |
Senthil Kumaran | e55a205 | 2010-06-16 14:55:31 +0000 | [diff] [blame] | 102 | |
Georg Brandl | 0ada39c | 2010-07-02 07:33:50 +0000 | [diff] [blame] | 103 | httpd = SocketServer.TCPServer(("", PORT), Handler) |
Senthil Kumaran | e55a205 | 2010-06-16 14:55:31 +0000 | [diff] [blame] | 104 | |
Georg Brandl | 0ada39c | 2010-07-02 07:33:50 +0000 | [diff] [blame] | 105 | print "serving at port", PORT |
| 106 | httpd.serve_forever() |
Senthil Kumaran | e55a205 | 2010-06-16 14:55:31 +0000 | [diff] [blame] | 107 | |
Georg Brandl | 0ada39c | 2010-07-02 07:33:50 +0000 | [diff] [blame] | 108 | The :mod:`SimpleHTTPServer` module can also be invoked directly using the |
Georg Brandl | b550b00 | 2010-07-03 08:40:13 +0000 | [diff] [blame] | 109 | :option:`-m` switch of the interpreter with a ``port number`` argument. |
Georg Brandl | 0ada39c | 2010-07-02 07:33:50 +0000 | [diff] [blame] | 110 | Similar to the previous example, this serves the files relative to the |
| 111 | current directory. :: |
Senthil Kumaran | e55a205 | 2010-06-16 14:55:31 +0000 | [diff] [blame] | 112 | |
Georg Brandl | 0ada39c | 2010-07-02 07:33:50 +0000 | [diff] [blame] | 113 | python -m SimpleHTTPServer 8000 |
Senthil Kumaran | e55a205 | 2010-06-16 14:55:31 +0000 | [diff] [blame] | 114 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 115 | .. seealso:: |
| 116 | |
| 117 | Module :mod:`BaseHTTPServer` |
| 118 | Base class implementation for Web server and request handler. |
| 119 | |