annotate Doc/lib/libsimplehttp.tex @ 37850:ebe59cd18273

Use explicit relative import for an, ehm, relative import.
author Thomas Wouters <thomas@python.org>
date Tue, 18 Apr 2006 21:41:36 +0000
parents 4668df450672
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10982
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1 \section{\module{SimpleHTTPServer} ---
15140
fb9958933b25 Better synopsis based on suggestion from Ka-Ping Yee <ping@lfw.org>.
Fred Drake <fdrake@acm.org>
parents: 10982
diff changeset
2 Simple HTTP request handler}
10982
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
3
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
4 \declaremodule{standard}{SimpleHTTPServer}
15467
d7864345b1c8 Update Moshe Zadka's email address to one works and might last a while.
Fred Drake <fdrake@acm.org>
parents: 15140
diff changeset
5 \sectionauthor{Moshe Zadka}{moshez@zadka.site.co.il}
15140
fb9958933b25 Better synopsis based on suggestion from Ka-Ping Yee <ping@lfw.org>.
Fred Drake <fdrake@acm.org>
parents: 10982
diff changeset
6 \modulesynopsis{This module provides a basic request handler for HTTP
fb9958933b25 Better synopsis based on suggestion from Ka-Ping Yee <ping@lfw.org>.
Fred Drake <fdrake@acm.org>
parents: 10982
diff changeset
7 servers.}
10982
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
8
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
9
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
10 The \module{SimpleHTTPServer} module defines a request-handler class,
33016
5c940f98e465 Various minor edits
Andrew M. Kuchling <amk@amk.ca>
parents: 33015
diff changeset
11 interface-compatible with \class{BaseHTTPServer.BaseHTTPRequestHandler},
5c940f98e465 Various minor edits
Andrew M. Kuchling <amk@amk.ca>
parents: 33015
diff changeset
12 that serves files only from a base directory.
10982
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
13
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
14 The \module{SimpleHTTPServer} module defines the following class:
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
15
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
16 \begin{classdesc}{SimpleHTTPRequestHandler}{request, client_address, server}
33016
5c940f98e465 Various minor edits
Andrew M. Kuchling <amk@amk.ca>
parents: 33015
diff changeset
17 This class is used to serve files from the current directory and below,
10982
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
18 directly mapping the directory structure to HTTP requests.
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
19
33016
5c940f98e465 Various minor edits
Andrew M. Kuchling <amk@amk.ca>
parents: 33015
diff changeset
20 A lot of the work, such as parsing the request, is done by the base
5c940f98e465 Various minor edits
Andrew M. Kuchling <amk@amk.ca>
parents: 33015
diff changeset
21 class \class{BaseHTTPServer.BaseHTTPRequestHandler}. This class
5c940f98e465 Various minor edits
Andrew M. Kuchling <amk@amk.ca>
parents: 33015
diff changeset
22 implements the \function{do_GET()} and \function{do_HEAD()} functions.
10982
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
23 \end{classdesc}
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
24
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
25 The \class{SimpleHTTPRequestHandler} defines the following member
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
26 variables:
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
27
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
28 \begin{memberdesc}{server_version}
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
29 This will be \code{"SimpleHTTP/" + __version__}, where \code{__version__}
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
30 is defined in the module.
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
31 \end{memberdesc}
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
32
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
33 \begin{memberdesc}{extensions_map}
33016
5c940f98e465 Various minor edits
Andrew M. Kuchling <amk@amk.ca>
parents: 33015
diff changeset
34 A dictionary mapping suffixes into MIME types. The default is signified
33015
6aa31d75c4f9 [Bug #866222] Update docs to match the module
Andrew M. Kuchling <amk@amk.ca>
parents: 15467
diff changeset
35 by an empty string, and is considered to be \code{application/octet-stream}.
10982
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
36 The mapping is used case-insensitively, and so should contain only
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
37 lower-cased keys.
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
38 \end{memberdesc}
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
39
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
40 The \class{SimpleHTTPRequestHandler} defines the following methods:
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
41
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
42 \begin{methoddesc}{do_HEAD}{}
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
43 This method serves the \code{'HEAD'} request type: it sends the
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
44 headers it would send for the equivalent \code{GET} request. See the
33016
5c940f98e465 Various minor edits
Andrew M. Kuchling <amk@amk.ca>
parents: 33015
diff changeset
45 \method{do_GET()} method for a more complete explanation of the possible
10982
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
46 headers.
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
47 \end{methoddesc}
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
48
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
49 \begin{methoddesc}{do_GET}{}
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
50 The request is mapped to a local file by interpreting the request as
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
51 a path relative to the current working directory.
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
52
33015
6aa31d75c4f9 [Bug #866222] Update docs to match the module
Andrew M. Kuchling <amk@amk.ca>
parents: 15467
diff changeset
53 If the request was mapped to a directory, the directory is checked for
6aa31d75c4f9 [Bug #866222] Update docs to match the module
Andrew M. Kuchling <amk@amk.ca>
parents: 15467
diff changeset
54 a file named \code{index.html} or \code{index.htm} (in that order).
6aa31d75c4f9 [Bug #866222] Update docs to match the module
Andrew M. Kuchling <amk@amk.ca>
parents: 15467
diff changeset
55 If found, the file's contents are returned; otherwise a directory
6aa31d75c4f9 [Bug #866222] Update docs to match the module
Andrew M. Kuchling <amk@amk.ca>
parents: 15467
diff changeset
56 listing is generated by calling the \method{list_directory()} method.
6aa31d75c4f9 [Bug #866222] Update docs to match the module
Andrew M. Kuchling <amk@amk.ca>
parents: 15467
diff changeset
57 This method uses \function{os.listdir()} to scan the directory, and
6aa31d75c4f9 [Bug #866222] Update docs to match the module
Andrew M. Kuchling <amk@amk.ca>
parents: 15467
diff changeset
58 returns a \code{404} error response if the \function{listdir()} fails.
6aa31d75c4f9 [Bug #866222] Update docs to match the module
Andrew M. Kuchling <amk@amk.ca>
parents: 15467
diff changeset
59
6aa31d75c4f9 [Bug #866222] Update docs to match the module
Andrew M. Kuchling <amk@amk.ca>
parents: 15467
diff changeset
60 If the request was mapped to a file, it is opened and the contents are
6aa31d75c4f9 [Bug #866222] Update docs to match the module
Andrew M. Kuchling <amk@amk.ca>
parents: 15467
diff changeset
61 returned. Any \exception{IOError} exception in opening the requested
6aa31d75c4f9 [Bug #866222] Update docs to match the module
Andrew M. Kuchling <amk@amk.ca>
parents: 15467
diff changeset
62 file is mapped to a \code{404}, \code{'File not found'}
33016
5c940f98e465 Various minor edits
Andrew M. Kuchling <amk@amk.ca>
parents: 33015
diff changeset
63 error. Otherwise, the content type is guessed by calling the
5c940f98e465 Various minor edits
Andrew M. Kuchling <amk@amk.ca>
parents: 33015
diff changeset
64 \method{guess_type()} method, which in turn uses the
33015
6aa31d75c4f9 [Bug #866222] Update docs to match the module
Andrew M. Kuchling <amk@amk.ca>
parents: 15467
diff changeset
65 \var{extensions_map} variable.
10982
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
66
33016
5c940f98e465 Various minor edits
Andrew M. Kuchling <amk@amk.ca>
parents: 33015
diff changeset
67 A \code{'Content-type:'} header with the guessed content type is
36555
4668df450672 Patch #1417555: SimpleHTTPServer now returns Last-Modified headers.
Georg Brandl <georg@python.org>
parents: 33016
diff changeset
68 output, followed by a \code{'Content-Length:'} header with the file's
4668df450672 Patch #1417555: SimpleHTTPServer now returns Last-Modified headers.
Georg Brandl <georg@python.org>
parents: 33016
diff changeset
69 size and a \code{'Last-Modified:'} header with the file's modification
4668df450672 Patch #1417555: SimpleHTTPServer now returns Last-Modified headers.
Georg Brandl <georg@python.org>
parents: 33016
diff changeset
70 time.
4668df450672 Patch #1417555: SimpleHTTPServer now returns Last-Modified headers.
Georg Brandl <georg@python.org>
parents: 33016
diff changeset
71
4668df450672 Patch #1417555: SimpleHTTPServer now returns Last-Modified headers.
Georg Brandl <georg@python.org>
parents: 33016
diff changeset
72 Then follows a blank line signifying the end of the headers,
33016
5c940f98e465 Various minor edits
Andrew M. Kuchling <amk@amk.ca>
parents: 33015
diff changeset
73 and then the contents of the file are output. If the file's MIME type
5c940f98e465 Various minor edits
Andrew M. Kuchling <amk@amk.ca>
parents: 33015
diff changeset
74 starts with \code{text/} the file is opened in text mode; otherwise
5c940f98e465 Various minor edits
Andrew M. Kuchling <amk@amk.ca>
parents: 33015
diff changeset
75 binary mode is used.
10982
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
76
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
77 For example usage, see the implementation of the \function{test()}
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
78 function.
36555
4668df450672 Patch #1417555: SimpleHTTPServer now returns Last-Modified headers.
Georg Brandl <georg@python.org>
parents: 33016
diff changeset
79 \versionadded[The \code{'Last-Modified'} header]{2.5}
10982
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
80 \end{methoddesc}
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
81
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
82
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
83 \begin{seealso}
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
84 \seemodule{BaseHTTPServer}{Base class implementation for Web server
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
85 and request handler.}
bef46ea5ef75 New sections from Moshe Zadka <moshez@math.huji.ac.il>.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
86 \end{seealso}