| 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 <[email protected]> |
| 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 |
.. warning:: |
| 15 |
|
| 16 |
:mod:`SimpleHTTPServer` is not recommended for production. It only implements |
| 17 |
basic security checks. |
| 18 |
|
| 19 |
The :mod:`SimpleHTTPServer` module defines a single class, |
| 20 |
:class:`SimpleHTTPRequestHandler`, which is interface-compatible with |
| 21 |
:class:`BaseHTTPServer.BaseHTTPRequestHandler`. |
| 22 |
|
| 23 |
The :mod:`SimpleHTTPServer` module defines the following class: |
| 24 |
|
| 25 |
|
| 26 |
.. class:: SimpleHTTPRequestHandler(request, client_address, server) |
| 27 |
|
| 28 |
This class serves files from the current directory and below, directly |
| 29 |
mapping the directory structure to HTTP requests. |
| 30 |
|
| 31 |
A lot of the work, such as parsing the request, is done by the base class |
| 32 |
:class:`BaseHTTPServer.BaseHTTPRequestHandler`. This class implements the |
| 33 |
:func:`do_GET` and :func:`do_HEAD` functions. |
| 34 |
|
| 35 |
The following are defined as class-level attributes of |
| 36 |
:class:`SimpleHTTPRequestHandler`: |
| 37 |
|
| 38 |
|
| 39 |
.. attribute:: server_version |
| 40 |
|
| 41 |
This will be ``"SimpleHTTP/" + __version__``, where ``__version__`` is |
| 42 |
defined at the module level. |
| 43 |
|
| 44 |
|
| 45 |
.. attribute:: extensions_map |
| 46 |
|
| 47 |
A dictionary mapping suffixes into MIME types. The default is |
| 48 |
signified by an empty string, and is considered to be |
| 49 |
``application/octet-stream``. The mapping is used case-insensitively, |
| 50 |
and so should contain only lower-cased keys. |
| 51 |
|
| 52 |
The :class:`SimpleHTTPRequestHandler` class defines the following methods: |
| 53 |
|
| 54 |
|
| 55 |
.. method:: do_HEAD() |
| 56 |
|
| 57 |
This method serves the ``'HEAD'`` request type: it sends the headers it |
| 58 |
would send for the equivalent ``GET`` request. See the :meth:`do_GET` |
| 59 |
method for a more complete explanation of the possible headers. |
| 60 |
|
| 61 |
|
| 62 |
.. method:: do_GET() |
| 63 |
|
| 64 |
The request is mapped to a local file by interpreting the request as a |
| 65 |
path relative to the current working directory. |
| 66 |
|
| 67 |
If the request was mapped to a directory, the directory is checked for a |
| 68 |
file named ``index.html`` or ``index.htm`` (in that order). If found, the |
| 69 |
file's contents are returned; otherwise a directory listing is generated |
| 70 |
by calling the :meth:`list_directory` method. This method uses |
| 71 |
:func:`os.listdir` to scan the directory, and returns a ``404`` error |
| 72 |
response if the :func:`listdir` fails. |
| 73 |
|
| 74 |
If the request was mapped to a file, it is opened and the contents are |
| 75 |
returned. Any :exc:`IOError` exception in opening the requested file is |
| 76 |
mapped to a ``404``, ``'File not found'`` error. Otherwise, the content |
| 77 |
type is guessed by calling the :meth:`guess_type` method, which in turn |
| 78 |
uses the *extensions_map* variable. |
| 79 |
|
| 80 |
A ``'Content-type:'`` header with the guessed content type is output, |
| 81 |
followed by a ``'Content-Length:'`` header with the file's size and a |
| 82 |
``'Last-Modified:'`` header with the file's modification time. |
| 83 |
|
| 84 |
Then follows a blank line signifying the end of the headers, and then the |
| 85 |
contents of the file are output. If the file's MIME type starts with |
| 86 |
``text/`` the file is opened in text mode; otherwise binary mode is used. |
| 87 |
|
| 88 |
The :func:`test` function in the :mod:`SimpleHTTPServer` module is an |
| 89 |
example which creates a server using the :class:`SimpleHTTPRequestHandler` |
| 90 |
as the Handler. |
| 91 |
|
| 92 |
.. versionadded:: 2.5 |
| 93 |
The ``'Last-Modified'`` header. |
| 94 |
|
| 95 |
|
| 96 |
The :mod:`SimpleHTTPServer` module can be used in the following manner in order |
| 97 |
to set up a very basic web server serving files relative to the current |
| 98 |
directory. :: |
| 99 |
|
| 100 |
import SimpleHTTPServer |
| 101 |
import SocketServer |
| 102 |
|
| 103 |
PORT = 8000 |
| 104 |
|
| 105 |
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler |
| 106 |
|
| 107 |
httpd = SocketServer.TCPServer(("", PORT), Handler) |
| 108 |
|
| 109 |
print "serving at port", PORT |
| 110 |
httpd.serve_forever() |
| 111 |
|
| 112 |
The :mod:`SimpleHTTPServer` module can also be invoked directly using the |
| 113 |
:option:`-m` switch of the interpreter with a ``port number`` argument. |
| 114 |
Similar to the previous example, this serves the files relative to the |
| 115 |
current directory. :: |
| 116 |
|
| 117 |
python -m SimpleHTTPServer 8000 |
| 118 |
|
| 119 |
.. seealso:: |
| 120 |
|
| 121 |
Module :mod:`BaseHTTPServer` |
| 122 |
Base class implementation for Web server and request handler. |
| 123 |
|