Fred Drake | 21572fd | 1999-06-14 19:47:47 +0000 | [diff] [blame] | 1 | \section{\module{SimpleHTTPServer} --- |
| 2 | A Do-Something Request Handler} |
| 3 | |
| 4 | \declaremodule{standard}{SimpleHTTPServer} |
| 5 | \sectionauthor{Moshe Zadka}{mzadka@geocities.com} |
| 6 | \modulesynopsis{This module provides a request handler for HTTP servers.} |
| 7 | |
| 8 | |
| 9 | The \module{SimpleHTTPServer} module defines a request-handler class, |
| 10 | interface compatible with \class{BaseHTTPServer.BaseHTTPRequestHandler} |
| 11 | which serves files only from a base directory. |
| 12 | |
| 13 | The \module{SimpleHTTPServer} module defines the following class: |
| 14 | |
| 15 | \begin{classdesc}{SimpleHTTPRequestHandler}{request, client_address, server} |
| 16 | This class is used, to serve files from current directory and below, |
| 17 | directly mapping the directory structure to HTTP requests. |
| 18 | |
| 19 | A lot of the work is done by the base class |
| 20 | \class{BaseHTTPServer.BaseHTTPRequestHandler}, such as parsing the |
| 21 | request. This class implements the \function{do_GET()} and |
| 22 | \function{do_HEAD()} functions. |
| 23 | \end{classdesc} |
| 24 | |
| 25 | The \class{SimpleHTTPRequestHandler} defines the following member |
| 26 | variables: |
| 27 | |
| 28 | \begin{memberdesc}{server_version} |
| 29 | This will be \code{"SimpleHTTP/" + __version__}, where \code{__version__} |
| 30 | is defined in the module. |
| 31 | \end{memberdesc} |
| 32 | |
| 33 | \begin{memberdesc}{extensions_map} |
| 34 | A dictionary mapping suffixes into MIME types. Default is signified |
| 35 | by an empty string, and is considered to be \code{text/plain}. |
| 36 | The mapping is used case-insensitively, and so should contain only |
| 37 | lower-cased keys. |
| 38 | \end{memberdesc} |
| 39 | |
| 40 | The \class{SimpleHTTPRequestHandler} defines the following methods: |
| 41 | |
| 42 | \begin{methoddesc}{do_HEAD}{} |
| 43 | This method serves the \code{'HEAD'} request type: it sends the |
| 44 | headers it would send for the equivalent \code{GET} request. See the |
| 45 | \method{do_GET()} method for more complete explanation of the possible |
| 46 | headers. |
| 47 | \end{methoddesc} |
| 48 | |
| 49 | \begin{methoddesc}{do_GET}{} |
| 50 | The request is mapped to a local file by interpreting the request as |
| 51 | a path relative to the current working directory. |
| 52 | |
| 53 | If the request was mapped to a directory, a \code{403} respond is output, |
| 54 | followed by the explanation \code{'Directory listing not supported'}. |
| 55 | Any \exception{IOError} exception in opening the requested file, is mapped |
| 56 | to a \code{404}, \code{'File not found'} error. Otherwise, the content |
| 57 | type is guessed using the \var{extensions_map} variable. |
| 58 | |
| 59 | A \code{'Content-type:'} with the guessed content type is output, and |
| 60 | then a blank line, signifying end of headers, and then the contents of |
| 61 | the file. The file is always opened in binary mode. |
| 62 | |
| 63 | For example usage, see the implementation of the \function{test()} |
| 64 | function. |
| 65 | \end{methoddesc} |
| 66 | |
| 67 | |
| 68 | \begin{seealso} |
| 69 | \seemodule{BaseHTTPServer}{Base class implementation for Web server |
| 70 | and request handler.} |
| 71 | \end{seealso} |