source: vendor/python/2.5/Doc/lib/libsimplehttp.tex

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

File size: 3.4 KB
Line 
1\section{\module{SimpleHTTPServer} ---
2 Simple HTTP request handler}
3
4\declaremodule{standard}{SimpleHTTPServer}
5\sectionauthor{Moshe Zadka}{moshez@zadka.site.co.il}
6\modulesynopsis{This module provides a basic request handler for HTTP
7 servers.}
8
9
10The \module{SimpleHTTPServer} module defines a request-handler class,
11interface-compatible with \class{BaseHTTPServer.BaseHTTPRequestHandler},
12that serves files only from a base directory.
13
14The \module{SimpleHTTPServer} module defines the following class:
15
16\begin{classdesc}{SimpleHTTPRequestHandler}{request, client_address, server}
17This class is used to serve files from the current directory and below,
18directly mapping the directory structure to HTTP requests.
19
20A lot of the work, such as parsing the request, is done by the base
21class \class{BaseHTTPServer.BaseHTTPRequestHandler}. This class
22implements the \function{do_GET()} and \function{do_HEAD()} functions.
23\end{classdesc}
24
25The \class{SimpleHTTPRequestHandler} defines the following member
26variables:
27
28\begin{memberdesc}{server_version}
29This will be \code{"SimpleHTTP/" + __version__}, where \code{__version__}
30is defined in the module.
31\end{memberdesc}
32
33\begin{memberdesc}{extensions_map}
34A dictionary mapping suffixes into MIME types. The default is signified
35by an empty string, and is considered to be \code{application/octet-stream}.
36The mapping is used case-insensitively, and so should contain only
37lower-cased keys.
38\end{memberdesc}
39
40The \class{SimpleHTTPRequestHandler} defines the following methods:
41
42\begin{methoddesc}{do_HEAD}{}
43This method serves the \code{'HEAD'} request type: it sends the
44headers it would send for the equivalent \code{GET} request. See the
45\method{do_GET()} method for a more complete explanation of the possible
46headers.
47\end{methoddesc}
48
49\begin{methoddesc}{do_GET}{}
50The request is mapped to a local file by interpreting the request as
51a path relative to the current working directory.
52
53If the request was mapped to a directory, the directory is checked for
54a file named \code{index.html} or \code{index.htm} (in that order).
55If found, the file's contents are returned; otherwise a directory
56listing is generated by calling the \method{list_directory()} method.
57This method uses \function{os.listdir()} to scan the directory, and
58returns a \code{404} error response if the \function{listdir()} fails.
59
60If the request was mapped to a file, it is opened and the contents are
61returned. Any \exception{IOError} exception in opening the requested
62file is mapped to a \code{404}, \code{'File not found'}
63error. Otherwise, the content type is guessed by calling the
64\method{guess_type()} method, which in turn uses the
65\var{extensions_map} variable.
66
67A \code{'Content-type:'} header with the guessed content type is
68output, followed by a \code{'Content-Length:'} header with the file's
69size and a \code{'Last-Modified:'} header with the file's modification
70time.
71
72Then follows a blank line signifying the end of the headers,
73and then the contents of the file are output. If the file's MIME type
74starts with \code{text/} the file is opened in text mode; otherwise
75binary mode is used.
76
77For example usage, see the implementation of the \function{test()}
78function.
79\versionadded[The \code{'Last-Modified'} header]{2.5}
80\end{methoddesc}
81
82
83\begin{seealso}
84 \seemodule{BaseHTTPServer}{Base class implementation for Web server
85 and request handler.}
86\end{seealso}
Note: See TracBrowser for help on using the repository browser.