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