blob: e409f91233c5bfd051914e17a7e58a33617b90d4 [file] [log] [blame]
Fred Drake21572fd1999-06-14 19:47:47 +00001\section{\module{SimpleHTTPServer} ---
Fred Drake25407882000-10-10 16:59:53 +00002 Simple HTTP request handler}
Fred Drake21572fd1999-06-14 19:47:47 +00003
4\declaremodule{standard}{SimpleHTTPServer}
Fred Drake57657bc2000-12-01 15:25:23 +00005\sectionauthor{Moshe Zadka}{moshez@zadka.site.co.il}
Fred Drake25407882000-10-10 16:59:53 +00006\modulesynopsis{This module provides a basic request handler for HTTP
7 servers.}
Fred Drake21572fd1999-06-14 19:47:47 +00008
9
10The \module{SimpleHTTPServer} module defines a request-handler class,
11interface compatible with \class{BaseHTTPServer.BaseHTTPRequestHandler}
12which 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 current directory and below,
18directly mapping the directory structure to HTTP requests.
19
20A lot of the work is done by the base class
21\class{BaseHTTPServer.BaseHTTPRequestHandler}, such as parsing the
22request. This class implements the \function{do_GET()} and
23\function{do_HEAD()} functions.
24\end{classdesc}
25
26The \class{SimpleHTTPRequestHandler} defines the following member
27variables:
28
29\begin{memberdesc}{server_version}
30This will be \code{"SimpleHTTP/" + __version__}, where \code{__version__}
31is defined in the module.
32\end{memberdesc}
33
34\begin{memberdesc}{extensions_map}
35A dictionary mapping suffixes into MIME types. Default is signified
Andrew M. Kuchling4cbe95c2004-08-07 19:06:48 +000036by an empty string, and is considered to be \code{application/octet-stream}.
Fred Drake21572fd1999-06-14 19:47:47 +000037The mapping is used case-insensitively, and so should contain only
38lower-cased keys.
39\end{memberdesc}
40
41The \class{SimpleHTTPRequestHandler} defines the following methods:
42
43\begin{methoddesc}{do_HEAD}{}
44This method serves the \code{'HEAD'} request type: it sends the
45headers it would send for the equivalent \code{GET} request. See the
46\method{do_GET()} method for more complete explanation of the possible
47headers.
48\end{methoddesc}
49
50\begin{methoddesc}{do_GET}{}
51The request is mapped to a local file by interpreting the request as
52a path relative to the current working directory.
53
Andrew M. Kuchling4cbe95c2004-08-07 19:06:48 +000054If the request was mapped to a directory, the directory is checked for
55a file named \code{index.html} or \code{index.htm} (in that order).
56If found, the file's contents are returned; otherwise a directory
57listing is generated by calling the \method{list_directory()} method.
58This method uses \function{os.listdir()} to scan the directory, and
59returns a \code{404} error response if the \function{listdir()} fails.
60
61If the request was mapped to a file, it is opened and the contents are
62returned. Any \exception{IOError} exception in opening the requested
63file is mapped to a \code{404}, \code{'File not found'}
64error. Otherwise, the content type is guessed using the
65\var{extensions_map} variable.
Fred Drake21572fd1999-06-14 19:47:47 +000066
67A \code{'Content-type:'} with the guessed content type is output, and
68then a blank line, signifying end of headers, and then the contents of
Andrew M. Kuchling4cbe95c2004-08-07 19:06:48 +000069the file. If the file's MIME type starts with \code{text/} the file is
70opened in text mode; otherwise binary mode is used.
Fred Drake21572fd1999-06-14 19:47:47 +000071
72For example usage, see the implementation of the \function{test()}
73function.
74\end{methoddesc}
75
76
77\begin{seealso}
78 \seemodule{BaseHTTPServer}{Base class implementation for Web server
79 and request handler.}
80\end{seealso}