Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 | :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 Brandl | 8de9119 | 2008-05-26 15:01:48 +0000 | [diff] [blame^] | 9 | .. note:: |
| 10 | The :mod:`CGIHTTPServer` module has been merged into :mod:`http.server` in |
| 11 | Python 3.0. The :term:`2to3` tool will automatically adapt imports when |
| 12 | converting your sources to 3.0. |
| 13 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 14 | |
| 15 | The :mod:`CGIHTTPServer` module defines a request-handler class, interface |
| 16 | compatible with :class:`BaseHTTPServer.BaseHTTPRequestHandler` and inherits |
| 17 | behavior from :class:`SimpleHTTPServer.SimpleHTTPRequestHandler` but can also |
| 18 | run CGI scripts. |
| 19 | |
| 20 | .. note:: |
| 21 | |
| 22 | This module can run CGI scripts on Unix and Windows systems; on Mac OS it will |
| 23 | only be able to run Python scripts within the same process as itself. |
| 24 | |
| 25 | .. note:: |
| 26 | |
| 27 | CGI scripts run by the :class:`CGIHTTPRequestHandler` class cannot execute |
| 28 | redirects (HTTP code 302), because code 200 (script output follows) is sent |
| 29 | prior to execution of the CGI script. This pre-empts the status code. |
| 30 | |
| 31 | The :mod:`CGIHTTPServer` module defines the following class: |
| 32 | |
| 33 | |
| 34 | .. class:: CGIHTTPRequestHandler(request, client_address, server) |
| 35 | |
| 36 | This class is used to serve either files or output of CGI scripts from the |
| 37 | current directory and below. Note that mapping HTTP hierarchic structure to |
| 38 | local directory structure is exactly as in |
| 39 | :class:`SimpleHTTPServer.SimpleHTTPRequestHandler`. |
| 40 | |
| 41 | The class will however, run the CGI script, instead of serving it as a file, if |
| 42 | it guesses it to be a CGI script. Only directory-based CGI are used --- the |
| 43 | other common server configuration is to treat special extensions as denoting CGI |
| 44 | scripts. |
| 45 | |
| 46 | The :func:`do_GET` and :func:`do_HEAD` functions are modified to run CGI scripts |
| 47 | and serve the output, instead of serving files, if the request leads to |
| 48 | somewhere below the ``cgi_directories`` path. |
| 49 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 50 | The :class:`CGIHTTPRequestHandler` defines the following data member: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 51 | |
| 52 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 53 | .. attribute:: cgi_directories |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 54 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 55 | This defaults to ``['/cgi-bin', '/htbin']`` and describes directories to |
| 56 | treat as containing CGI scripts. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 57 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 58 | The :class:`CGIHTTPRequestHandler` defines the following methods: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 59 | |
| 60 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 61 | .. method:: do_POST() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 62 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 63 | This method serves the ``'POST'`` request type, only allowed for CGI |
| 64 | scripts. Error 501, "Can only POST to CGI scripts", is output when trying |
| 65 | to POST to a non-CGI url. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 66 | |
| 67 | Note that CGI scripts will be run with UID of user nobody, for security reasons. |
| 68 | Problems with the CGI script will be translated to error 403. |
| 69 | |
| 70 | For example usage, see the implementation of the :func:`test` function. |
| 71 | |
| 72 | |
| 73 | .. seealso:: |
| 74 | |
| 75 | Module :mod:`BaseHTTPServer` |
| 76 | Base class implementation for Web server and request handler. |
| 77 | |