Fred Drake | 21572fd | 1999-06-14 19:47:47 +0000 | [diff] [blame] | 1 | \section{\module{CGIHTTPServer} --- |
Fred Drake | 4d3714b | 2000-10-10 16:56:41 +0000 | [diff] [blame] | 2 | CGI-capable HTTP request handler} |
Fred Drake | 21572fd | 1999-06-14 19:47:47 +0000 | [diff] [blame] | 3 | |
| 4 | |
| 5 | \declaremodule{standard}{CGIHTTPServer} |
Fred Drake | 57657bc | 2000-12-01 15:25:23 +0000 | [diff] [blame] | 6 | \sectionauthor{Moshe Zadka}{moshez@zadka.site.co.il} |
Fred Drake | 21572fd | 1999-06-14 19:47:47 +0000 | [diff] [blame] | 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 |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 13 | \class{BaseHTTPServer.BaseHTTPRequestHandler} and inherits behavior |
Fred Drake | 21572fd | 1999-06-14 19:47:47 +0000 | [diff] [blame] | 14 | from \class{SimpleHTTPServer.SimpleHTTPRequestHandler} but can also |
| 15 | run CGI scripts. |
| 16 | |
Fred Drake | e70dbe0 | 2001-11-19 05:16:35 +0000 | [diff] [blame] | 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.} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 20 | |
Jeremy Hylton | 6414cd8 | 2004-12-22 14:19:09 +0000 | [diff] [blame] | 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 | |
Fred Drake | 21572fd | 1999-06-14 19:47:47 +0000 | [diff] [blame] | 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} |