blob: 6275c1a4d18a7fb6450aa7d75f25b022ea05c1a4 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2:mod:`CGIHTTPServer` --- CGI-capable HTTP request handler
3=========================================================
4
5.. module:: CGIHTTPServer
6 :synopsis: This module provides a request handler for HTTP servers which can run CGI
7 scripts.
8.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
9
10
11The :mod:`CGIHTTPServer` module defines a request-handler class, interface
12compatible with :class:`BaseHTTPServer.BaseHTTPRequestHandler` and inherits
13behavior from :class:`SimpleHTTPServer.SimpleHTTPRequestHandler` but can also
14run CGI scripts.
15
16.. note::
17
18 This module can run CGI scripts on Unix and Windows systems; on Mac OS it will
19 only be able to run Python scripts within the same process as itself.
20
21.. note::
22
23 CGI scripts run by the :class:`CGIHTTPRequestHandler` class cannot execute
24 redirects (HTTP code 302), because code 200 (script output follows) is sent
25 prior to execution of the CGI script. This pre-empts the status code.
26
27The :mod:`CGIHTTPServer` module defines the following class:
28
29
30.. class:: CGIHTTPRequestHandler(request, client_address, server)
31
32 This class is used to serve either files or output of CGI scripts from the
33 current directory and below. Note that mapping HTTP hierarchic structure to
34 local directory structure is exactly as in
35 :class:`SimpleHTTPServer.SimpleHTTPRequestHandler`.
36
37 The class will however, run the CGI script, instead of serving it as a file, if
38 it guesses it to be a CGI script. Only directory-based CGI are used --- the
39 other common server configuration is to treat special extensions as denoting CGI
40 scripts.
41
42 The :func:`do_GET` and :func:`do_HEAD` functions are modified to run CGI scripts
43 and serve the output, instead of serving files, if the request leads to
44 somewhere below the ``cgi_directories`` path.
45
Benjamin Petersonc7b05922008-04-25 01:29:10 +000046 The :class:`CGIHTTPRequestHandler` defines the following data member:
Georg Brandl8ec7f652007-08-15 14:28:01 +000047
48
Benjamin Petersonc7b05922008-04-25 01:29:10 +000049 .. attribute:: cgi_directories
Georg Brandl8ec7f652007-08-15 14:28:01 +000050
Benjamin Petersonc7b05922008-04-25 01:29:10 +000051 This defaults to ``['/cgi-bin', '/htbin']`` and describes directories to
52 treat as containing CGI scripts.
Georg Brandl8ec7f652007-08-15 14:28:01 +000053
Benjamin Petersonc7b05922008-04-25 01:29:10 +000054 The :class:`CGIHTTPRequestHandler` defines the following methods:
Georg Brandl8ec7f652007-08-15 14:28:01 +000055
56
Benjamin Petersonc7b05922008-04-25 01:29:10 +000057 .. method:: do_POST()
Georg Brandl8ec7f652007-08-15 14:28:01 +000058
Benjamin Petersonc7b05922008-04-25 01:29:10 +000059 This method serves the ``'POST'`` request type, only allowed for CGI
60 scripts. Error 501, "Can only POST to CGI scripts", is output when trying
61 to POST to a non-CGI url.
Georg Brandl8ec7f652007-08-15 14:28:01 +000062
63Note that CGI scripts will be run with UID of user nobody, for security reasons.
64Problems with the CGI script will be translated to error 403.
65
66For example usage, see the implementation of the :func:`test` function.
67
68
69.. seealso::
70
71 Module :mod:`BaseHTTPServer`
72 Base class implementation for Web server and request handler.
73