blob: c30eaa83475a4f57efadf03b121be8adad5c60b5 [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,
Andrew M. Kuchling59a27f12004-08-07 19:10:36 +000011interface-compatible with \class{BaseHTTPServer.BaseHTTPRequestHandler},
12that serves files only from a base directory.
Fred Drake21572fd1999-06-14 19:47:47 +000013
14The \module{SimpleHTTPServer} module defines the following class:
15
16\begin{classdesc}{SimpleHTTPRequestHandler}{request, client_address, server}
Andrew M. Kuchling59a27f12004-08-07 19:10:36 +000017This class is used to serve files from the current directory and below,
Fred Drake21572fd1999-06-14 19:47:47 +000018directly mapping the directory structure to HTTP requests.
19
Andrew M. Kuchling59a27f12004-08-07 19:10:36 +000020A 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.
Fred Drake21572fd1999-06-14 19:47:47 +000023\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}
Andrew M. Kuchling59a27f12004-08-07 19:10:36 +000034A dictionary mapping suffixes into MIME types. The default is signified
Andrew M. Kuchling4cbe95c2004-08-07 19:06:48 +000035by an empty string, and is considered to be \code{application/octet-stream}.
Fred Drake21572fd1999-06-14 19:47:47 +000036The 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
Andrew M. Kuchling59a27f12004-08-07 19:10:36 +000045\method{do_GET()} method for a more complete explanation of the possible
Fred Drake21572fd1999-06-14 19:47:47 +000046headers.
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
Andrew M. Kuchling4cbe95c2004-08-07 19:06:48 +000053If 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'}
Andrew M. Kuchling59a27f12004-08-07 19:10:36 +000063error. Otherwise, the content type is guessed by calling the
64\method{guess_type()} method, which in turn uses the
Andrew M. Kuchling4cbe95c2004-08-07 19:06:48 +000065\var{extensions_map} variable.
Fred Drake21572fd1999-06-14 19:47:47 +000066
Andrew M. Kuchling59a27f12004-08-07 19:10:36 +000067A \code{'Content-type:'} header with the guessed content type is
68output, followed by a blank line signifying the end of the headers,
69and then the contents of the file are output. If the file's MIME type
70starts with \code{text/} the file is opened in text mode; otherwise
71binary mode is used.
Fred Drake21572fd1999-06-14 19:47:47 +000072
73For example usage, see the implementation of the \function{test()}
74function.
75\end{methoddesc}
76
77
78\begin{seealso}
79 \seemodule{BaseHTTPServer}{Base class implementation for Web server
80 and request handler.}
81\end{seealso}