/Doc/library/cgihttpserver.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 76 lines · 48 code · 28 blank · 0 comment · 0 complexity · eab78f9dfafbe1ff0afd66cd56748a2b MD5 · raw file

  1. :mod:`CGIHTTPServer` --- CGI-capable HTTP request handler
  2. =========================================================
  3. .. module:: CGIHTTPServer
  4. :synopsis: This module provides a request handler for HTTP servers which can run CGI
  5. scripts.
  6. .. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
  7. .. note::
  8. The :mod:`CGIHTTPServer` module has been merged into :mod:`http.server` in
  9. Python 3.0. The :term:`2to3` tool will automatically adapt imports when
  10. converting your sources to 3.0.
  11. The :mod:`CGIHTTPServer` module defines a request-handler class, interface
  12. compatible with :class:`BaseHTTPServer.BaseHTTPRequestHandler` and inherits
  13. behavior from :class:`SimpleHTTPServer.SimpleHTTPRequestHandler` but can also
  14. run CGI scripts.
  15. .. note::
  16. This module can run CGI scripts on Unix and Windows systems.
  17. .. note::
  18. CGI scripts run by the :class:`CGIHTTPRequestHandler` class cannot execute
  19. redirects (HTTP code 302), because code 200 (script output follows) is sent
  20. prior to execution of the CGI script. This pre-empts the status code.
  21. The :mod:`CGIHTTPServer` module defines the following class:
  22. .. class:: CGIHTTPRequestHandler(request, client_address, server)
  23. This class is used to serve either files or output of CGI scripts from the
  24. current directory and below. Note that mapping HTTP hierarchic structure to
  25. local directory structure is exactly as in
  26. :class:`SimpleHTTPServer.SimpleHTTPRequestHandler`.
  27. The class will however, run the CGI script, instead of serving it as a file, if
  28. it guesses it to be a CGI script. Only directory-based CGI are used --- the
  29. other common server configuration is to treat special extensions as denoting CGI
  30. scripts.
  31. The :func:`do_GET` and :func:`do_HEAD` functions are modified to run CGI scripts
  32. and serve the output, instead of serving files, if the request leads to
  33. somewhere below the ``cgi_directories`` path.
  34. The :class:`CGIHTTPRequestHandler` defines the following data member:
  35. .. attribute:: cgi_directories
  36. This defaults to ``['/cgi-bin', '/htbin']`` and describes directories to
  37. treat as containing CGI scripts.
  38. The :class:`CGIHTTPRequestHandler` defines the following methods:
  39. .. method:: do_POST()
  40. This method serves the ``'POST'`` request type, only allowed for CGI
  41. scripts. Error 501, "Can only POST to CGI scripts", is output when trying
  42. to POST to a non-CGI url.
  43. Note that CGI scripts will be run with UID of user nobody, for security reasons.
  44. Problems with the CGI script will be translated to error 403.
  45. For example usage, see the implementation of the :func:`test` function.
  46. .. seealso::
  47. Module :mod:`BaseHTTPServer`
  48. Base class implementation for Web server and request handler.