| 1 | \section{\module{CGIHTTPServer} ---
|
|---|
| 2 | CGI-capable HTTP request handler}
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 | \declaremodule{standard}{CGIHTTPServer}
|
|---|
| 6 | \sectionauthor{Moshe Zadka}{[email protected]}
|
|---|
| 7 | \modulesynopsis{This module provides a request handler for HTTP servers
|
|---|
| 8 | which can run CGI scripts.}
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 | The \module{CGIHTTPServer} module defines a request-handler class,
|
|---|
| 12 | interface compatible with
|
|---|
| 13 | \class{BaseHTTPServer.BaseHTTPRequestHandler} and inherits behavior
|
|---|
| 14 | from \class{SimpleHTTPServer.SimpleHTTPRequestHandler} but can also
|
|---|
| 15 | run CGI scripts.
|
|---|
| 16 |
|
|---|
| 17 | \note{This module can run CGI scripts on \UNIX{} and Windows systems;
|
|---|
| 18 | on Mac OS it will only be able to run Python scripts within the same
|
|---|
| 19 | process as itself.}
|
|---|
| 20 |
|
|---|
| 21 | \note{CGI scripts run by the \class{CGIHTTPRequestHandler} class cannot execute
|
|---|
| 22 | redirects (HTTP code 302), because code 200 (script output follows)
|
|---|
| 23 | is sent prior to execution of the CGI script. This pre-empts the status
|
|---|
| 24 | code.}
|
|---|
| 25 |
|
|---|
| 26 | The \module{CGIHTTPServer} module defines the following class:
|
|---|
| 27 |
|
|---|
| 28 | \begin{classdesc}{CGIHTTPRequestHandler}{request, client_address, server}
|
|---|
| 29 | This class is used to serve either files or output of CGI scripts from
|
|---|
| 30 | the current directory and below. Note that mapping HTTP hierarchic
|
|---|
| 31 | structure to local directory structure is exactly as in
|
|---|
| 32 | \class{SimpleHTTPServer.SimpleHTTPRequestHandler}.
|
|---|
| 33 |
|
|---|
| 34 | The class will however, run the CGI script, instead of serving it as a
|
|---|
| 35 | file, if it guesses it to be a CGI script. Only directory-based CGI
|
|---|
| 36 | are used --- the other common server configuration is to treat special
|
|---|
| 37 | extensions as denoting CGI scripts.
|
|---|
| 38 |
|
|---|
| 39 | The \function{do_GET()} and \function{do_HEAD()} functions are
|
|---|
| 40 | modified to run CGI scripts and serve the output, instead of serving
|
|---|
| 41 | files, if the request leads to somewhere below the
|
|---|
| 42 | \code{cgi_directories} path.
|
|---|
| 43 | \end{classdesc}
|
|---|
| 44 |
|
|---|
| 45 | The \class{CGIHTTPRequestHandler} defines the following data member:
|
|---|
| 46 |
|
|---|
| 47 | \begin{memberdesc}{cgi_directories}
|
|---|
| 48 | This defaults to \code{['/cgi-bin', '/htbin']} and describes
|
|---|
| 49 | directories to treat as containing CGI scripts.
|
|---|
| 50 | \end{memberdesc}
|
|---|
| 51 |
|
|---|
| 52 | The \class{CGIHTTPRequestHandler} defines the following methods:
|
|---|
| 53 |
|
|---|
| 54 | \begin{methoddesc}{do_POST}{}
|
|---|
| 55 | This method serves the \code{'POST'} request type, only allowed for
|
|---|
| 56 | CGI scripts. Error 501, "Can only POST to CGI scripts", is output
|
|---|
| 57 | when trying to POST to a non-CGI url.
|
|---|
| 58 | \end{methoddesc}
|
|---|
| 59 |
|
|---|
| 60 | Note that CGI scripts will be run with UID of user nobody, for security
|
|---|
| 61 | reasons. Problems with the CGI script will be translated to error 403.
|
|---|
| 62 |
|
|---|
| 63 | For example usage, see the implementation of the \function{test()}
|
|---|
| 64 | function.
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 | \begin{seealso}
|
|---|
| 68 | \seemodule{BaseHTTPServer}{Base class implementation for Web server
|
|---|
| 69 | and request handler.}
|
|---|
| 70 | \end{seealso}
|
|---|