annotate Doc/library/simplehttpserver.rst @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 8aab13719c02
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
76732
8aab13719c02 Use Python 3 instead of 3.0.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62715
diff changeset
11 Python 3. The :term:`2to3` tool will automatically adapt imports when
8aab13719c02 Use Python 3 instead of 3.0.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62715
diff changeset
12 converting your sources to Python 3.
47310
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
62677
ba9cf0df21b0 Grammar and markup fixes.
Georg Brandl <georg@python.org>
parents: 62359
diff changeset
85 example which creates a server using the :class:`SimpleHTTPRequestHandler`
ba9cf0df21b0 Grammar and markup fixes.
Georg Brandl <georg@python.org>
parents: 62359
diff changeset
86 as the Handler.
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
62677
ba9cf0df21b0 Grammar and markup fixes.
Georg Brandl <georg@python.org>
parents: 62359
diff changeset
92 The :mod:`SimpleHTTPServer` module can be used in the following manner in order
ba9cf0df21b0 Grammar and markup fixes.
Georg Brandl <georg@python.org>
parents: 62359
diff changeset
93 to set up a very basic web server serving files relative to the current
ba9cf0df21b0 Grammar and markup fixes.
Georg Brandl <georg@python.org>
parents: 62359
diff changeset
94 directory. ::
62354
84fefbf1d6c0 Fix Issue8937 - SimpleHTTPServer should contain usage example
Senthil Kumaran <orsenthil@gmail.com>
parents: 47310
diff changeset
95
62677
ba9cf0df21b0 Grammar and markup fixes.
Georg Brandl <georg@python.org>
parents: 62359
diff changeset
96 import SimpleHTTPServer
ba9cf0df21b0 Grammar and markup fixes.
Georg Brandl <georg@python.org>
parents: 62359
diff changeset
97 import SocketServer
62354
84fefbf1d6c0 Fix Issue8937 - SimpleHTTPServer should contain usage example
Senthil Kumaran <orsenthil@gmail.com>
parents: 47310
diff changeset
98
62677
ba9cf0df21b0 Grammar and markup fixes.
Georg Brandl <georg@python.org>
parents: 62359
diff changeset
99 PORT = 8000
62354
84fefbf1d6c0 Fix Issue8937 - SimpleHTTPServer should contain usage example
Senthil Kumaran <orsenthil@gmail.com>
parents: 47310
diff changeset
100
62677
ba9cf0df21b0 Grammar and markup fixes.
Georg Brandl <georg@python.org>
parents: 62359
diff changeset
101 Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
62354
84fefbf1d6c0 Fix Issue8937 - SimpleHTTPServer should contain usage example
Senthil Kumaran <orsenthil@gmail.com>
parents: 47310
diff changeset
102
62677
ba9cf0df21b0 Grammar and markup fixes.
Georg Brandl <georg@python.org>
parents: 62359
diff changeset
103 httpd = SocketServer.TCPServer(("", PORT), Handler)
62354
84fefbf1d6c0 Fix Issue8937 - SimpleHTTPServer should contain usage example
Senthil Kumaran <orsenthil@gmail.com>
parents: 47310
diff changeset
104
62677
ba9cf0df21b0 Grammar and markup fixes.
Georg Brandl <georg@python.org>
parents: 62359
diff changeset
105 print "serving at port", PORT
ba9cf0df21b0 Grammar and markup fixes.
Georg Brandl <georg@python.org>
parents: 62359
diff changeset
106 httpd.serve_forever()
62354
84fefbf1d6c0 Fix Issue8937 - SimpleHTTPServer should contain usage example
Senthil Kumaran <orsenthil@gmail.com>
parents: 47310
diff changeset
107
62677
ba9cf0df21b0 Grammar and markup fixes.
Georg Brandl <georg@python.org>
parents: 62359
diff changeset
108 The :mod:`SimpleHTTPServer` module can also be invoked directly using the
62715
32396e80048f Fix role name.
Georg Brandl <georg@python.org>
parents: 62677
diff changeset
109 :option:`-m` switch of the interpreter with a ``port number`` argument.
62677
ba9cf0df21b0 Grammar and markup fixes.
Georg Brandl <georg@python.org>
parents: 62359
diff changeset
110 Similar to the previous example, this serves the files relative to the
ba9cf0df21b0 Grammar and markup fixes.
Georg Brandl <georg@python.org>
parents: 62359
diff changeset
111 current directory. ::
62354
84fefbf1d6c0 Fix Issue8937 - SimpleHTTPServer should contain usage example
Senthil Kumaran <orsenthil@gmail.com>
parents: 47310
diff changeset
112
62677
ba9cf0df21b0 Grammar and markup fixes.
Georg Brandl <georg@python.org>
parents: 62359
diff changeset
113 python -m SimpleHTTPServer 8000
62354
84fefbf1d6c0 Fix Issue8937 - SimpleHTTPServer should contain usage example
Senthil Kumaran <orsenthil@gmail.com>
parents: 47310
diff changeset
114
42604
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
115 .. seealso::
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
116
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
117 Module :mod:`BaseHTTPServer`
9e1529bf0442 Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff changeset
118 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
119