Mercurial > cpython-lru_cache-weakref
annotate Doc/library/simplehttpserver.rst @ 62354:84fefbf1d6c0 legacy-trunk
Fix Issue8937 - SimpleHTTPServer should contain usage example
| author | Senthil Kumaran <orsenthil@gmail.com> |
|---|---|
| date | Wed, 16 Jun 2010 14:55:31 +0000 |
| parents | 662d15af52df |
| children | c6845e83086f |
| rev | line source |
|---|---|
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
1 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
2 :mod:`SimpleHTTPServer` --- Simple HTTP request handler |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
3 ======================================================= |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
4 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
5 .. module:: SimpleHTTPServer |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
6 :synopsis: This module provides a basic request handler for HTTP servers. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
7 .. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il> |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
8 |
|
47310
662d15af52df
Add renaming notices to 3.0 http package members.
Georg Brandl <georg@python.org>
parents:
46581
diff
changeset
|
9 .. note:: |
|
662d15af52df
Add renaming notices to 3.0 http package members.
Georg Brandl <georg@python.org>
parents:
46581
diff
changeset
|
10 The :mod:`SimpleHTTPServer` module has been merged into :mod:`http.server` in |
|
662d15af52df
Add renaming notices to 3.0 http package members.
Georg Brandl <georg@python.org>
parents:
46581
diff
changeset
|
11 Python 3.0. The :term:`2to3` tool will automatically adapt imports when |
|
662d15af52df
Add renaming notices to 3.0 http package members.
Georg Brandl <georg@python.org>
parents:
46581
diff
changeset
|
12 converting your sources to 3.0. |
|
662d15af52df
Add renaming notices to 3.0 http package members.
Georg Brandl <georg@python.org>
parents:
46581
diff
changeset
|
13 |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
14 |
|
46581
3d9ddce2da85
minor wording changes, rewrap a few lines
Skip Montanaro <skip@pobox.com>
parents:
46555
diff
changeset
|
15 The :mod:`SimpleHTTPServer` module defines a single class, |
|
3d9ddce2da85
minor wording changes, rewrap a few lines
Skip Montanaro <skip@pobox.com>
parents:
46555
diff
changeset
|
16 :class:`SimpleHTTPRequestHandler`, which is interface-compatible with |
|
3d9ddce2da85
minor wording changes, rewrap a few lines
Skip Montanaro <skip@pobox.com>
parents:
46555
diff
changeset
|
17 :class:`BaseHTTPServer.BaseHTTPRequestHandler`. |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
18 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
19 The :mod:`SimpleHTTPServer` module defines the following class: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
20 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
21 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
22 .. class:: SimpleHTTPRequestHandler(request, client_address, server) |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
23 |
|
46581
3d9ddce2da85
minor wording changes, rewrap a few lines
Skip Montanaro <skip@pobox.com>
parents:
46555
diff
changeset
|
24 This class serves files from the current directory and below, directly |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
25 mapping the directory structure to HTTP requests. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
26 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
27 A lot of the work, such as parsing the request, is done by the base class |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
28 :class:`BaseHTTPServer.BaseHTTPRequestHandler`. This class implements the |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
29 :func:`do_GET` and :func:`do_HEAD` functions. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
30 |
|
46581
3d9ddce2da85
minor wording changes, rewrap a few lines
Skip Montanaro <skip@pobox.com>
parents:
46555
diff
changeset
|
31 The following are defined as class-level attributes of |
|
3d9ddce2da85
minor wording changes, rewrap a few lines
Skip Montanaro <skip@pobox.com>
parents:
46555
diff
changeset
|
32 :class:`SimpleHTTPRequestHandler`: |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
33 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
34 |
|
46555
66c8bd1bedf3
reformat some documentation of classes so methods and attributes are under the class directive
Benjamin Peterson <benjamin@python.org>
parents:
42604
diff
changeset
|
35 .. attribute:: server_version |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
36 |
|
46581
3d9ddce2da85
minor wording changes, rewrap a few lines
Skip Montanaro <skip@pobox.com>
parents:
46555
diff
changeset
|
37 This will be ``"SimpleHTTP/" + __version__``, where ``__version__`` is |
|
3d9ddce2da85
minor wording changes, rewrap a few lines
Skip Montanaro <skip@pobox.com>
parents:
46555
diff
changeset
|
38 defined at the module level. |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
39 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
40 |
|
46555
66c8bd1bedf3
reformat some documentation of classes so methods and attributes are under the class directive
Benjamin Peterson <benjamin@python.org>
parents:
42604
diff
changeset
|
41 .. attribute:: extensions_map |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
42 |
|
46581
3d9ddce2da85
minor wording changes, rewrap a few lines
Skip Montanaro <skip@pobox.com>
parents:
46555
diff
changeset
|
43 A dictionary mapping suffixes into MIME types. The default is |
|
3d9ddce2da85
minor wording changes, rewrap a few lines
Skip Montanaro <skip@pobox.com>
parents:
46555
diff
changeset
|
44 signified by an empty string, and is considered to be |
|
3d9ddce2da85
minor wording changes, rewrap a few lines
Skip Montanaro <skip@pobox.com>
parents:
46555
diff
changeset
|
45 ``application/octet-stream``. The mapping is used case-insensitively, |
|
3d9ddce2da85
minor wording changes, rewrap a few lines
Skip Montanaro <skip@pobox.com>
parents:
46555
diff
changeset
|
46 and so should contain only lower-cased keys. |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
47 |
|
46581
3d9ddce2da85
minor wording changes, rewrap a few lines
Skip Montanaro <skip@pobox.com>
parents:
46555
diff
changeset
|
48 The :class:`SimpleHTTPRequestHandler` class defines the following methods: |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
49 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
50 |
|
46555
66c8bd1bedf3
reformat some documentation of classes so methods and attributes are under the class directive
Benjamin Peterson <benjamin@python.org>
parents:
42604
diff
changeset
|
51 .. method:: do_HEAD() |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
52 |
|
46555
66c8bd1bedf3
reformat some documentation of classes so methods and attributes are under the class directive
Benjamin Peterson <benjamin@python.org>
parents:
42604
diff
changeset
|
53 This method serves the ``'HEAD'`` request type: it sends the headers it |
|
66c8bd1bedf3
reformat some documentation of classes so methods and attributes are under the class directive
Benjamin Peterson <benjamin@python.org>
parents:
42604
diff
changeset
|
54 would send for the equivalent ``GET`` request. See the :meth:`do_GET` |
|
66c8bd1bedf3
reformat some documentation of classes so methods and attributes are under the class directive
Benjamin Peterson <benjamin@python.org>
parents:
42604
diff
changeset
|
55 method for a more complete explanation of the possible headers. |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
56 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
57 |
|
46555
66c8bd1bedf3
reformat some documentation of classes so methods and attributes are under the class directive
Benjamin Peterson <benjamin@python.org>
parents:
42604
diff
changeset
|
58 .. method:: do_GET() |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
59 |
|
46555
66c8bd1bedf3
reformat some documentation of classes so methods and attributes are under the class directive
Benjamin Peterson <benjamin@python.org>
parents:
42604
diff
changeset
|
60 The request is mapped to a local file by interpreting the request as a |
|
66c8bd1bedf3
reformat some documentation of classes so methods and attributes are under the class directive
Benjamin Peterson <benjamin@python.org>
parents:
42604
diff
changeset
|
61 path relative to the current working directory. |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
62 |
|
46555
66c8bd1bedf3
reformat some documentation of classes so methods and attributes are under the class directive
Benjamin Peterson <benjamin@python.org>
parents:
42604
diff
changeset
|
63 If the request was mapped to a directory, the directory is checked for a |
|
66c8bd1bedf3
reformat some documentation of classes so methods and attributes are under the class directive
Benjamin Peterson <benjamin@python.org>
parents:
42604
diff
changeset
|
64 file named ``index.html`` or ``index.htm`` (in that order). If found, the |
|
66c8bd1bedf3
reformat some documentation of classes so methods and attributes are under the class directive
Benjamin Peterson <benjamin@python.org>
parents:
42604
diff
changeset
|
65 file's contents are returned; otherwise a directory listing is generated |
|
66c8bd1bedf3
reformat some documentation of classes so methods and attributes are under the class directive
Benjamin Peterson <benjamin@python.org>
parents:
42604
diff
changeset
|
66 by calling the :meth:`list_directory` method. This method uses |
|
66c8bd1bedf3
reformat some documentation of classes so methods and attributes are under the class directive
Benjamin Peterson <benjamin@python.org>
parents:
42604
diff
changeset
|
67 :func:`os.listdir` to scan the directory, and returns a ``404`` error |
|
66c8bd1bedf3
reformat some documentation of classes so methods and attributes are under the class directive
Benjamin Peterson <benjamin@python.org>
parents:
42604
diff
changeset
|
68 response if the :func:`listdir` fails. |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
69 |
|
46555
66c8bd1bedf3
reformat some documentation of classes so methods and attributes are under the class directive
Benjamin Peterson <benjamin@python.org>
parents:
42604
diff
changeset
|
70 If the request was mapped to a file, it is opened and the contents are |
|
66c8bd1bedf3
reformat some documentation of classes so methods and attributes are under the class directive
Benjamin Peterson <benjamin@python.org>
parents:
42604
diff
changeset
|
71 returned. Any :exc:`IOError` exception in opening the requested file is |
|
66c8bd1bedf3
reformat some documentation of classes so methods and attributes are under the class directive
Benjamin Peterson <benjamin@python.org>
parents:
42604
diff
changeset
|
72 mapped to a ``404``, ``'File not found'`` error. Otherwise, the content |
|
66c8bd1bedf3
reformat some documentation of classes so methods and attributes are under the class directive
Benjamin Peterson <benjamin@python.org>
parents:
42604
diff
changeset
|
73 type is guessed by calling the :meth:`guess_type` method, which in turn |
|
66c8bd1bedf3
reformat some documentation of classes so methods and attributes are under the class directive
Benjamin Peterson <benjamin@python.org>
parents:
42604
diff
changeset
|
74 uses the *extensions_map* variable. |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
75 |
|
46555
66c8bd1bedf3
reformat some documentation of classes so methods and attributes are under the class directive
Benjamin Peterson <benjamin@python.org>
parents:
42604
diff
changeset
|
76 A ``'Content-type:'`` header with the guessed content type is output, |
|
66c8bd1bedf3
reformat some documentation of classes so methods and attributes are under the class directive
Benjamin Peterson <benjamin@python.org>
parents:
42604
diff
changeset
|
77 followed by a ``'Content-Length:'`` header with the file's size and a |
|
66c8bd1bedf3
reformat some documentation of classes so methods and attributes are under the class directive
Benjamin Peterson <benjamin@python.org>
parents:
42604
diff
changeset
|
78 ``'Last-Modified:'`` header with the file's modification time. |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
79 |
|
46555
66c8bd1bedf3
reformat some documentation of classes so methods and attributes are under the class directive
Benjamin Peterson <benjamin@python.org>
parents:
42604
diff
changeset
|
80 Then follows a blank line signifying the end of the headers, and then the |
|
66c8bd1bedf3
reformat some documentation of classes so methods and attributes are under the class directive
Benjamin Peterson <benjamin@python.org>
parents:
42604
diff
changeset
|
81 contents of the file are output. If the file's MIME type starts with |
|
66c8bd1bedf3
reformat some documentation of classes so methods and attributes are under the class directive
Benjamin Peterson <benjamin@python.org>
parents:
42604
diff
changeset
|
82 ``text/`` the file is opened in text mode; otherwise binary mode is used. |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
83 |
|
62354
84fefbf1d6c0
Fix Issue8937 - SimpleHTTPServer should contain usage example
Senthil Kumaran <orsenthil@gmail.com>
parents:
47310
diff
changeset
|
84 The :func:`test` function in the :mod:`SimpleHTTPServer` module is an |
|
84fefbf1d6c0
Fix Issue8937 - SimpleHTTPServer should contain usage example
Senthil Kumaran <orsenthil@gmail.com>
parents:
47310
diff
changeset
|
85 example which interfaces the :class:`SimpleHTTPRequestHandler` as a |
|
84fefbf1d6c0
Fix Issue8937 - SimpleHTTPServer should contain usage example
Senthil Kumaran <orsenthil@gmail.com>
parents:
47310
diff
changeset
|
86 Handler to the :mod:`BaseHTTPServer` module. |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
87 |
|
46555
66c8bd1bedf3
reformat some documentation of classes so methods and attributes are under the class directive
Benjamin Peterson <benjamin@python.org>
parents:
42604
diff
changeset
|
88 .. versionadded:: 2.5 |
|
66c8bd1bedf3
reformat some documentation of classes so methods and attributes are under the class directive
Benjamin Peterson <benjamin@python.org>
parents:
42604
diff
changeset
|
89 The ``'Last-Modified'`` header. |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
90 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
91 |
|
62354
84fefbf1d6c0
Fix Issue8937 - SimpleHTTPServer should contain usage example
Senthil Kumaran <orsenthil@gmail.com>
parents:
47310
diff
changeset
|
92 The :mod:`SimpleHTTPServer` module can be used the following manner in order to |
|
84fefbf1d6c0
Fix Issue8937 - SimpleHTTPServer should contain usage example
Senthil Kumaran <orsenthil@gmail.com>
parents:
47310
diff
changeset
|
93 setup a very basic web server serving files relative to the current directory.:: |
|
84fefbf1d6c0
Fix Issue8937 - SimpleHTTPServer should contain usage example
Senthil Kumaran <orsenthil@gmail.com>
parents:
47310
diff
changeset
|
94 |
|
84fefbf1d6c0
Fix Issue8937 - SimpleHTTPServer should contain usage example
Senthil Kumaran <orsenthil@gmail.com>
parents:
47310
diff
changeset
|
95 import SimpleHTTPServer |
|
84fefbf1d6c0
Fix Issue8937 - SimpleHTTPServer should contain usage example
Senthil Kumaran <orsenthil@gmail.com>
parents:
47310
diff
changeset
|
96 import SocketServer |
|
84fefbf1d6c0
Fix Issue8937 - SimpleHTTPServer should contain usage example
Senthil Kumaran <orsenthil@gmail.com>
parents:
47310
diff
changeset
|
97 |
|
84fefbf1d6c0
Fix Issue8937 - SimpleHTTPServer should contain usage example
Senthil Kumaran <orsenthil@gmail.com>
parents:
47310
diff
changeset
|
98 PORT = 8000 |
|
84fefbf1d6c0
Fix Issue8937 - SimpleHTTPServer should contain usage example
Senthil Kumaran <orsenthil@gmail.com>
parents:
47310
diff
changeset
|
99 |
|
84fefbf1d6c0
Fix Issue8937 - SimpleHTTPServer should contain usage example
Senthil Kumaran <orsenthil@gmail.com>
parents:
47310
diff
changeset
|
100 Handler = SimpleHTTPServer.SimpleHTTPRequestHandler |
|
84fefbf1d6c0
Fix Issue8937 - SimpleHTTPServer should contain usage example
Senthil Kumaran <orsenthil@gmail.com>
parents:
47310
diff
changeset
|
101 |
|
84fefbf1d6c0
Fix Issue8937 - SimpleHTTPServer should contain usage example
Senthil Kumaran <orsenthil@gmail.com>
parents:
47310
diff
changeset
|
102 httpd = SocketServer.TCPServer(("", PORT), Handler) |
|
84fefbf1d6c0
Fix Issue8937 - SimpleHTTPServer should contain usage example
Senthil Kumaran <orsenthil@gmail.com>
parents:
47310
diff
changeset
|
103 |
|
84fefbf1d6c0
Fix Issue8937 - SimpleHTTPServer should contain usage example
Senthil Kumaran <orsenthil@gmail.com>
parents:
47310
diff
changeset
|
104 print "serving at port", PORT |
|
84fefbf1d6c0
Fix Issue8937 - SimpleHTTPServer should contain usage example
Senthil Kumaran <orsenthil@gmail.com>
parents:
47310
diff
changeset
|
105 httpd.serve_forever() |
|
84fefbf1d6c0
Fix Issue8937 - SimpleHTTPServer should contain usage example
Senthil Kumaran <orsenthil@gmail.com>
parents:
47310
diff
changeset
|
106 |
|
84fefbf1d6c0
Fix Issue8937 - SimpleHTTPServer should contain usage example
Senthil Kumaran <orsenthil@gmail.com>
parents:
47310
diff
changeset
|
107 It can also be invoked directly using the ``-m`` switch of interpreter a with |
|
84fefbf1d6c0
Fix Issue8937 - SimpleHTTPServer should contain usage example
Senthil Kumaran <orsenthil@gmail.com>
parents:
47310
diff
changeset
|
108 ``port number`` argument.:: |
|
84fefbf1d6c0
Fix Issue8937 - SimpleHTTPServer should contain usage example
Senthil Kumaran <orsenthil@gmail.com>
parents:
47310
diff
changeset
|
109 |
|
84fefbf1d6c0
Fix Issue8937 - SimpleHTTPServer should contain usage example
Senthil Kumaran <orsenthil@gmail.com>
parents:
47310
diff
changeset
|
110 python -m SimpleHTTPServer 8000 |
|
84fefbf1d6c0
Fix Issue8937 - SimpleHTTPServer should contain usage example
Senthil Kumaran <orsenthil@gmail.com>
parents:
47310
diff
changeset
|
111 |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
112 .. seealso:: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
113 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
114 Module :mod:`BaseHTTPServer` |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
115 Base class implementation for Web server and request handler. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
116 |
