blob: 013ee82546758327e4cc03bebe609fbdb93bb4c6 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001:mod:`CGIHTTPServer` --- CGI-capable HTTP request handler
2=========================================================
3
4.. module:: CGIHTTPServer
5 :synopsis: This module provides a request handler for HTTP servers which can run CGI
6 scripts.
7.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
8
Georg Brandl8de91192008-05-26 15:01:48 +00009.. note::
10 The :mod:`CGIHTTPServer` module has been merged into :mod:`http.server` in
Ezio Melotti510ff542012-05-03 19:21:40 +030011 Python 3. The :term:`2to3` tool will automatically adapt imports when
12 converting your sources to Python 3.
Georg Brandl8de91192008-05-26 15:01:48 +000013
Georg Brandl8ec7f652007-08-15 14:28:01 +000014
15The :mod:`CGIHTTPServer` module defines a request-handler class, interface
16compatible with :class:`BaseHTTPServer.BaseHTTPRequestHandler` and inherits
17behavior from :class:`SimpleHTTPServer.SimpleHTTPRequestHandler` but can also
18run CGI scripts.
19
20.. note::
21
Georg Brandl9af94982008-09-13 17:41:16 +000022 This module can run CGI scripts on Unix and Windows systems.
Georg Brandl8ec7f652007-08-15 14:28:01 +000023
24.. note::
25
26 CGI scripts run by the :class:`CGIHTTPRequestHandler` class cannot execute
27 redirects (HTTP code 302), because code 200 (script output follows) is sent
28 prior to execution of the CGI script. This pre-empts the status code.
29
30The :mod:`CGIHTTPServer` module defines the following class:
31
32
33.. class:: CGIHTTPRequestHandler(request, client_address, server)
34
35 This class is used to serve either files or output of CGI scripts from the
36 current directory and below. Note that mapping HTTP hierarchic structure to
37 local directory structure is exactly as in
38 :class:`SimpleHTTPServer.SimpleHTTPRequestHandler`.
39
40 The class will however, run the CGI script, instead of serving it as a file, if
41 it guesses it to be a CGI script. Only directory-based CGI are used --- the
42 other common server configuration is to treat special extensions as denoting CGI
43 scripts.
44
45 The :func:`do_GET` and :func:`do_HEAD` functions are modified to run CGI scripts
46 and serve the output, instead of serving files, if the request leads to
47 somewhere below the ``cgi_directories`` path.
48
Benjamin Petersonc7b05922008-04-25 01:29:10 +000049 The :class:`CGIHTTPRequestHandler` defines the following data member:
Georg Brandl8ec7f652007-08-15 14:28:01 +000050
51
Benjamin Petersonc7b05922008-04-25 01:29:10 +000052 .. attribute:: cgi_directories
Georg Brandl8ec7f652007-08-15 14:28:01 +000053
Benjamin Petersonc7b05922008-04-25 01:29:10 +000054 This defaults to ``['/cgi-bin', '/htbin']`` and describes directories to
55 treat as containing CGI scripts.
Georg Brandl8ec7f652007-08-15 14:28:01 +000056
Benjamin Petersonc7b05922008-04-25 01:29:10 +000057 The :class:`CGIHTTPRequestHandler` defines the following methods:
Georg Brandl8ec7f652007-08-15 14:28:01 +000058
59
Benjamin Petersonc7b05922008-04-25 01:29:10 +000060 .. method:: do_POST()
Georg Brandl8ec7f652007-08-15 14:28:01 +000061
Benjamin Petersonc7b05922008-04-25 01:29:10 +000062 This method serves the ``'POST'`` request type, only allowed for CGI
63 scripts. Error 501, "Can only POST to CGI scripts", is output when trying
64 to POST to a non-CGI url.
Georg Brandl8ec7f652007-08-15 14:28:01 +000065
66Note that CGI scripts will be run with UID of user nobody, for security reasons.
67Problems with the CGI script will be translated to error 403.
68
69For example usage, see the implementation of the :func:`test` function.
70
71
72.. seealso::
73
74 Module :mod:`BaseHTTPServer`
75 Base class implementation for Web server and request handler.
76