20.20.CGIHTTPServer — CGI-capable HTTP request handler

Note

TheCGIHTTPServer module has been merged intohttp.server inPython 3. The2to3 tool will automatically adapt imports whenconverting your sources to Python 3.

TheCGIHTTPServer module defines a request-handler class, interfacecompatible withBaseHTTPServer.BaseHTTPRequestHandler and inheritsbehavior fromSimpleHTTPServer.SimpleHTTPRequestHandler but can alsorun CGI scripts.

Note

This module can run CGI scripts on Unix and Windows systems.

Note

CGI scripts run by theCGIHTTPRequestHandler class cannot executeredirects (HTTP code 302), because code 200 (script output follows) is sentprior to execution of the CGI script. This pre-empts the status code.

TheCGIHTTPServer module defines the following class:

classCGIHTTPServer.CGIHTTPRequestHandler(request,client_address,server)

This class is used to serve either files or output of CGI scripts from thecurrent directory and below. Note that mapping HTTP hierarchic structure tolocal directory structure is exactly as inSimpleHTTPServer.SimpleHTTPRequestHandler.

The class will however, run the CGI script, instead of serving it as a file, ifit guesses it to be a CGI script. Only directory-based CGI are used — theother common server configuration is to treat special extensions as denoting CGIscripts.

Thedo_GET() anddo_HEAD() functions are modified to run CGI scriptsand serve the output, instead of serving files, if the request leads tosomewhere below thecgi_directories path.

TheCGIHTTPRequestHandler defines the following data member:

cgi_directories

This defaults to['/cgi-bin','/htbin'] and describes directories totreat as containing CGI scripts.

TheCGIHTTPRequestHandler defines the following methods:

do_POST()

This method serves the'POST' request type, only allowed for CGIscripts. Error 501, “Can only POST to CGI scripts”, is output when tryingto POST to a non-CGI url.

Note that CGI scripts will be run with UID of user nobody, for security reasons.Problems with the CGI script will be translated to error 403.

For example usage, see the implementation of thetest() function.

See also

ModuleBaseHTTPServer

Base class implementation for Web server and request handler.