17db96d56Sopenharmony_ci:mod:`cgi` --- Common Gateway Interface support
27db96d56Sopenharmony_ci===============================================
37db96d56Sopenharmony_ci
47db96d56Sopenharmony_ci.. module:: cgi
57db96d56Sopenharmony_ci   :synopsis: Helpers for running Python scripts via the Common Gateway Interface.
67db96d56Sopenharmony_ci   :deprecated:
77db96d56Sopenharmony_ci
87db96d56Sopenharmony_ci**Source code:** :source:`Lib/cgi.py`
97db96d56Sopenharmony_ci
107db96d56Sopenharmony_ci.. index::
117db96d56Sopenharmony_ci   pair: WWW; server
127db96d56Sopenharmony_ci   pair: CGI; protocol
137db96d56Sopenharmony_ci   pair: HTTP; protocol
147db96d56Sopenharmony_ci   pair: MIME; headers
157db96d56Sopenharmony_ci   single: URL
167db96d56Sopenharmony_ci   single: Common Gateway Interface
177db96d56Sopenharmony_ci
187db96d56Sopenharmony_ci.. deprecated-removed:: 3.11 3.13
197db96d56Sopenharmony_ci   The :mod:`cgi` module is deprecated
207db96d56Sopenharmony_ci   (see :pep:`PEP 594 <594#cgi>` for details and alternatives).
217db96d56Sopenharmony_ci
227db96d56Sopenharmony_ci   The :class:`FieldStorage` class can typically be replaced with
237db96d56Sopenharmony_ci   :func:`urllib.parse.parse_qsl` for ``GET`` and ``HEAD`` requests,
247db96d56Sopenharmony_ci   and the :mod:`email.message` module or
257db96d56Sopenharmony_ci   `multipart <https://pypi.org/project/multipart/>`_ for ``POST`` and ``PUT``.
267db96d56Sopenharmony_ci   Most :ref:`utility functions <functions-in-cgi-module>` have replacements.
277db96d56Sopenharmony_ci
287db96d56Sopenharmony_ci--------------
297db96d56Sopenharmony_ci
307db96d56Sopenharmony_ciSupport module for Common Gateway Interface (CGI) scripts.
317db96d56Sopenharmony_ci
327db96d56Sopenharmony_ciThis module defines a number of utilities for use by CGI scripts written in
337db96d56Sopenharmony_ciPython.
347db96d56Sopenharmony_ci
357db96d56Sopenharmony_ciThe global variable ``maxlen`` can be set to an integer indicating the maximum
367db96d56Sopenharmony_cisize of a POST request. POST requests larger than this size will result in a
377db96d56Sopenharmony_ci:exc:`ValueError` being raised during parsing. The default value of this
387db96d56Sopenharmony_civariable is ``0``, meaning the request size is unlimited.
397db96d56Sopenharmony_ci
407db96d56Sopenharmony_ci.. include:: ../includes/wasm-notavail.rst
417db96d56Sopenharmony_ci
427db96d56Sopenharmony_ciIntroduction
437db96d56Sopenharmony_ci------------
447db96d56Sopenharmony_ci
457db96d56Sopenharmony_ci.. _cgi-intro:
467db96d56Sopenharmony_ci
477db96d56Sopenharmony_ciA CGI script is invoked by an HTTP server, usually to process user input
487db96d56Sopenharmony_cisubmitted through an HTML ``<FORM>`` or ``<ISINDEX>`` element.
497db96d56Sopenharmony_ci
507db96d56Sopenharmony_ciMost often, CGI scripts live in the server's special :file:`cgi-bin` directory.
517db96d56Sopenharmony_ciThe HTTP server places all sorts of information about the request (such as the
527db96d56Sopenharmony_ciclient's hostname, the requested URL, the query string, and lots of other
537db96d56Sopenharmony_cigoodies) in the script's shell environment, executes the script, and sends the
547db96d56Sopenharmony_ciscript's output back to the client.
557db96d56Sopenharmony_ci
567db96d56Sopenharmony_ciThe script's input is connected to the client too, and sometimes the form data
577db96d56Sopenharmony_ciis read this way; at other times the form data is passed via the "query string"
587db96d56Sopenharmony_cipart of the URL.  This module is intended to take care of the different cases
597db96d56Sopenharmony_ciand provide a simpler interface to the Python script.  It also provides a number
607db96d56Sopenharmony_ciof utilities that help in debugging scripts, and the latest addition is support
617db96d56Sopenharmony_cifor file uploads from a form (if your browser supports it).
627db96d56Sopenharmony_ci
637db96d56Sopenharmony_ciThe output of a CGI script should consist of two sections, separated by a blank
647db96d56Sopenharmony_ciline.  The first section contains a number of headers, telling the client what
657db96d56Sopenharmony_cikind of data is following.  Python code to generate a minimal header section
667db96d56Sopenharmony_cilooks like this::
677db96d56Sopenharmony_ci
687db96d56Sopenharmony_ci   print("Content-Type: text/html")    # HTML is following
697db96d56Sopenharmony_ci   print()                             # blank line, end of headers
707db96d56Sopenharmony_ci
717db96d56Sopenharmony_ciThe second section is usually HTML, which allows the client software to display
727db96d56Sopenharmony_cinicely formatted text with header, in-line images, etc. Here's Python code that
737db96d56Sopenharmony_ciprints a simple piece of HTML::
747db96d56Sopenharmony_ci
757db96d56Sopenharmony_ci   print("<TITLE>CGI script output</TITLE>")
767db96d56Sopenharmony_ci   print("<H1>This is my first CGI script</H1>")
777db96d56Sopenharmony_ci   print("Hello, world!")
787db96d56Sopenharmony_ci
797db96d56Sopenharmony_ci
807db96d56Sopenharmony_ci.. _using-the-cgi-module:
817db96d56Sopenharmony_ci
827db96d56Sopenharmony_ciUsing the cgi module
837db96d56Sopenharmony_ci--------------------
847db96d56Sopenharmony_ci
857db96d56Sopenharmony_ciBegin by writing ``import cgi``.
867db96d56Sopenharmony_ci
877db96d56Sopenharmony_ciWhen you write a new script, consider adding these lines::
887db96d56Sopenharmony_ci
897db96d56Sopenharmony_ci   import cgitb
907db96d56Sopenharmony_ci   cgitb.enable()
917db96d56Sopenharmony_ci
927db96d56Sopenharmony_ciThis activates a special exception handler that will display detailed reports in
937db96d56Sopenharmony_cithe web browser if any errors occur.  If you'd rather not show the guts of your
947db96d56Sopenharmony_ciprogram to users of your script, you can have the reports saved to files
957db96d56Sopenharmony_ciinstead, with code like this::
967db96d56Sopenharmony_ci
977db96d56Sopenharmony_ci   import cgitb
987db96d56Sopenharmony_ci   cgitb.enable(display=0, logdir="/path/to/logdir")
997db96d56Sopenharmony_ci
1007db96d56Sopenharmony_ciIt's very helpful to use this feature during script development. The reports
1017db96d56Sopenharmony_ciproduced by :mod:`cgitb` provide information that can save you a lot of time in
1027db96d56Sopenharmony_citracking down bugs.  You can always remove the ``cgitb`` line later when you
1037db96d56Sopenharmony_cihave tested your script and are confident that it works correctly.
1047db96d56Sopenharmony_ci
1057db96d56Sopenharmony_ciTo get at submitted form data, use the :class:`FieldStorage` class. If the form
1067db96d56Sopenharmony_cicontains non-ASCII characters, use the *encoding* keyword parameter set to the
1077db96d56Sopenharmony_civalue of the encoding defined for the document. It is usually contained in the
1087db96d56Sopenharmony_ciMETA tag in the HEAD section of the HTML document or by the
1097db96d56Sopenharmony_ci:mailheader:`Content-Type` header.  This reads the form contents from the
1107db96d56Sopenharmony_cistandard input or the environment (depending on the value of various
1117db96d56Sopenharmony_cienvironment variables set according to the CGI standard).  Since it may consume
1127db96d56Sopenharmony_cistandard input, it should be instantiated only once.
1137db96d56Sopenharmony_ci
1147db96d56Sopenharmony_ciThe :class:`FieldStorage` instance can be indexed like a Python dictionary.
1157db96d56Sopenharmony_ciIt allows membership testing with the :keyword:`in` operator, and also supports
1167db96d56Sopenharmony_cithe standard dictionary method :meth:`~dict.keys` and the built-in function
1177db96d56Sopenharmony_ci:func:`len`.  Form fields containing empty strings are ignored and do not appear
1187db96d56Sopenharmony_ciin the dictionary; to keep such values, provide a true value for the optional
1197db96d56Sopenharmony_ci*keep_blank_values* keyword parameter when creating the :class:`FieldStorage`
1207db96d56Sopenharmony_ciinstance.
1217db96d56Sopenharmony_ci
1227db96d56Sopenharmony_ciFor instance, the following code (which assumes that the
1237db96d56Sopenharmony_ci:mailheader:`Content-Type` header and blank line have already been printed)
1247db96d56Sopenharmony_cichecks that the fields ``name`` and ``addr`` are both set to a non-empty
1257db96d56Sopenharmony_cistring::
1267db96d56Sopenharmony_ci
1277db96d56Sopenharmony_ci   form = cgi.FieldStorage()
1287db96d56Sopenharmony_ci   if "name" not in form or "addr" not in form:
1297db96d56Sopenharmony_ci       print("<H1>Error</H1>")
1307db96d56Sopenharmony_ci       print("Please fill in the name and addr fields.")
1317db96d56Sopenharmony_ci       return
1327db96d56Sopenharmony_ci   print("<p>name:", form["name"].value)
1337db96d56Sopenharmony_ci   print("<p>addr:", form["addr"].value)
1347db96d56Sopenharmony_ci   ...further form processing here...
1357db96d56Sopenharmony_ci
1367db96d56Sopenharmony_ciHere the fields, accessed through ``form[key]``, are themselves instances of
1377db96d56Sopenharmony_ci:class:`FieldStorage` (or :class:`MiniFieldStorage`, depending on the form
1387db96d56Sopenharmony_ciencoding). The :attr:`~FieldStorage.value` attribute of the instance yields
1397db96d56Sopenharmony_cithe string value of the field.  The :meth:`~FieldStorage.getvalue` method
1407db96d56Sopenharmony_cireturns this string value directly; it also accepts an optional second argument
1417db96d56Sopenharmony_cias a default to return if the requested key is not present.
1427db96d56Sopenharmony_ci
1437db96d56Sopenharmony_ciIf the submitted form data contains more than one field with the same name, the
1447db96d56Sopenharmony_ciobject retrieved by ``form[key]`` is not a :class:`FieldStorage` or
1457db96d56Sopenharmony_ci:class:`MiniFieldStorage` instance but a list of such instances.  Similarly, in
1467db96d56Sopenharmony_cithis situation, ``form.getvalue(key)`` would return a list of strings. If you
1477db96d56Sopenharmony_ciexpect this possibility (when your HTML form contains multiple fields with the
1487db96d56Sopenharmony_cisame name), use the :meth:`~FieldStorage.getlist` method, which always returns
1497db96d56Sopenharmony_cia list of values (so that you do not need to special-case the single item
1507db96d56Sopenharmony_cicase).  For example, this code concatenates any number of username fields,
1517db96d56Sopenharmony_ciseparated by commas::
1527db96d56Sopenharmony_ci
1537db96d56Sopenharmony_ci   value = form.getlist("username")
1547db96d56Sopenharmony_ci   usernames = ",".join(value)
1557db96d56Sopenharmony_ci
1567db96d56Sopenharmony_ciIf a field represents an uploaded file, accessing the value via the
1577db96d56Sopenharmony_ci:attr:`~FieldStorage.value` attribute or the :meth:`~FieldStorage.getvalue`
1587db96d56Sopenharmony_cimethod reads the entire file in memory as bytes.  This may not be what you
1597db96d56Sopenharmony_ciwant.  You can test for an uploaded file by testing either the
1607db96d56Sopenharmony_ci:attr:`~FieldStorage.filename` attribute or the :attr:`~FieldStorage.file`
1617db96d56Sopenharmony_ciattribute.  You can then read the data from the :attr:`!file`
1627db96d56Sopenharmony_ciattribute before it is automatically closed as part of the garbage collection of
1637db96d56Sopenharmony_cithe :class:`FieldStorage` instance
1647db96d56Sopenharmony_ci(the :func:`~io.RawIOBase.read` and :func:`~io.IOBase.readline` methods will
1657db96d56Sopenharmony_cireturn bytes)::
1667db96d56Sopenharmony_ci
1677db96d56Sopenharmony_ci   fileitem = form["userfile"]
1687db96d56Sopenharmony_ci   if fileitem.file:
1697db96d56Sopenharmony_ci       # It's an uploaded file; count lines
1707db96d56Sopenharmony_ci       linecount = 0
1717db96d56Sopenharmony_ci       while True:
1727db96d56Sopenharmony_ci           line = fileitem.file.readline()
1737db96d56Sopenharmony_ci           if not line: break
1747db96d56Sopenharmony_ci           linecount = linecount + 1
1757db96d56Sopenharmony_ci
1767db96d56Sopenharmony_ci:class:`FieldStorage` objects also support being used in a :keyword:`with`
1777db96d56Sopenharmony_cistatement, which will automatically close them when done.
1787db96d56Sopenharmony_ci
1797db96d56Sopenharmony_ciIf an error is encountered when obtaining the contents of an uploaded file
1807db96d56Sopenharmony_ci(for example, when the user interrupts the form submission by clicking on
1817db96d56Sopenharmony_cia Back or Cancel button) the :attr:`~FieldStorage.done` attribute of the
1827db96d56Sopenharmony_ciobject for the field will be set to the value -1.
1837db96d56Sopenharmony_ci
1847db96d56Sopenharmony_ciThe file upload draft standard entertains the possibility of uploading multiple
1857db96d56Sopenharmony_cifiles from one field (using a recursive :mimetype:`multipart/\*` encoding).
1867db96d56Sopenharmony_ciWhen this occurs, the item will be a dictionary-like :class:`FieldStorage` item.
1877db96d56Sopenharmony_ciThis can be determined by testing its :attr:`!type` attribute, which should be
1887db96d56Sopenharmony_ci:mimetype:`multipart/form-data` (or perhaps another MIME type matching
1897db96d56Sopenharmony_ci:mimetype:`multipart/\*`).  In this case, it can be iterated over recursively
1907db96d56Sopenharmony_cijust like the top-level form object.
1917db96d56Sopenharmony_ci
1927db96d56Sopenharmony_ciWhen a form is submitted in the "old" format (as the query string or as a single
1937db96d56Sopenharmony_cidata part of type :mimetype:`application/x-www-form-urlencoded`), the items will
1947db96d56Sopenharmony_ciactually be instances of the class :class:`MiniFieldStorage`.  In this case, the
1957db96d56Sopenharmony_ci:attr:`!list`, :attr:`!file`, and :attr:`filename` attributes are always ``None``.
1967db96d56Sopenharmony_ci
1977db96d56Sopenharmony_ciA form submitted via POST that also has a query string will contain both
1987db96d56Sopenharmony_ci:class:`FieldStorage` and :class:`MiniFieldStorage` items.
1997db96d56Sopenharmony_ci
2007db96d56Sopenharmony_ci.. versionchanged:: 3.4
2017db96d56Sopenharmony_ci   The :attr:`~FieldStorage.file` attribute is automatically closed upon the
2027db96d56Sopenharmony_ci   garbage collection of the creating :class:`FieldStorage` instance.
2037db96d56Sopenharmony_ci
2047db96d56Sopenharmony_ci.. versionchanged:: 3.5
2057db96d56Sopenharmony_ci   Added support for the context management protocol to the
2067db96d56Sopenharmony_ci   :class:`FieldStorage` class.
2077db96d56Sopenharmony_ci
2087db96d56Sopenharmony_ci
2097db96d56Sopenharmony_ciHigher Level Interface
2107db96d56Sopenharmony_ci----------------------
2117db96d56Sopenharmony_ci
2127db96d56Sopenharmony_ciThe previous section explains how to read CGI form data using the
2137db96d56Sopenharmony_ci:class:`FieldStorage` class.  This section describes a higher level interface
2147db96d56Sopenharmony_ciwhich was added to this class to allow one to do it in a more readable and
2157db96d56Sopenharmony_ciintuitive way.  The interface doesn't make the techniques described in previous
2167db96d56Sopenharmony_cisections obsolete --- they are still useful to process file uploads efficiently,
2177db96d56Sopenharmony_cifor example.
2187db96d56Sopenharmony_ci
2197db96d56Sopenharmony_ci.. XXX: Is this true ?
2207db96d56Sopenharmony_ci
2217db96d56Sopenharmony_ciThe interface consists of two simple methods. Using the methods you can process
2227db96d56Sopenharmony_ciform data in a generic way, without the need to worry whether only one or more
2237db96d56Sopenharmony_civalues were posted under one name.
2247db96d56Sopenharmony_ci
2257db96d56Sopenharmony_ciIn the previous section, you learned to write following code anytime you
2267db96d56Sopenharmony_ciexpected a user to post more than one value under one name::
2277db96d56Sopenharmony_ci
2287db96d56Sopenharmony_ci   item = form.getvalue("item")
2297db96d56Sopenharmony_ci   if isinstance(item, list):
2307db96d56Sopenharmony_ci       # The user is requesting more than one item.
2317db96d56Sopenharmony_ci   else:
2327db96d56Sopenharmony_ci       # The user is requesting only one item.
2337db96d56Sopenharmony_ci
2347db96d56Sopenharmony_ciThis situation is common for example when a form contains a group of multiple
2357db96d56Sopenharmony_cicheckboxes with the same name::
2367db96d56Sopenharmony_ci
2377db96d56Sopenharmony_ci   <input type="checkbox" name="item" value="1" />
2387db96d56Sopenharmony_ci   <input type="checkbox" name="item" value="2" />
2397db96d56Sopenharmony_ci
2407db96d56Sopenharmony_ciIn most situations, however, there's only one form control with a particular
2417db96d56Sopenharmony_ciname in a form and then you expect and need only one value associated with this
2427db96d56Sopenharmony_ciname.  So you write a script containing for example this code::
2437db96d56Sopenharmony_ci
2447db96d56Sopenharmony_ci   user = form.getvalue("user").upper()
2457db96d56Sopenharmony_ci
2467db96d56Sopenharmony_ciThe problem with the code is that you should never expect that a client will
2477db96d56Sopenharmony_ciprovide valid input to your scripts.  For example, if a curious user appends
2487db96d56Sopenharmony_cianother ``user=foo`` pair to the query string, then the script would crash,
2497db96d56Sopenharmony_cibecause in this situation the ``getvalue("user")`` method call returns a list
2507db96d56Sopenharmony_ciinstead of a string.  Calling the :meth:`~str.upper` method on a list is not valid
2517db96d56Sopenharmony_ci(since lists do not have a method of this name) and results in an
2527db96d56Sopenharmony_ci:exc:`AttributeError` exception.
2537db96d56Sopenharmony_ci
2547db96d56Sopenharmony_ciTherefore, the appropriate way to read form data values was to always use the
2557db96d56Sopenharmony_cicode which checks whether the obtained value is a single value or a list of
2567db96d56Sopenharmony_civalues.  That's annoying and leads to less readable scripts.
2577db96d56Sopenharmony_ci
2587db96d56Sopenharmony_ciA more convenient approach is to use the methods :meth:`~FieldStorage.getfirst`
2597db96d56Sopenharmony_ciand :meth:`~FieldStorage.getlist` provided by this higher level interface.
2607db96d56Sopenharmony_ci
2617db96d56Sopenharmony_ci
2627db96d56Sopenharmony_ci.. method:: FieldStorage.getfirst(name, default=None)
2637db96d56Sopenharmony_ci
2647db96d56Sopenharmony_ci   This method always returns only one value associated with form field *name*.
2657db96d56Sopenharmony_ci   The method returns only the first value in case that more values were posted
2667db96d56Sopenharmony_ci   under such name.  Please note that the order in which the values are received
2677db96d56Sopenharmony_ci   may vary from browser to browser and should not be counted on. [#]_  If no such
2687db96d56Sopenharmony_ci   form field or value exists then the method returns the value specified by the
2697db96d56Sopenharmony_ci   optional parameter *default*.  This parameter defaults to ``None`` if not
2707db96d56Sopenharmony_ci   specified.
2717db96d56Sopenharmony_ci
2727db96d56Sopenharmony_ci
2737db96d56Sopenharmony_ci.. method:: FieldStorage.getlist(name)
2747db96d56Sopenharmony_ci
2757db96d56Sopenharmony_ci   This method always returns a list of values associated with form field *name*.
2767db96d56Sopenharmony_ci   The method returns an empty list if no such form field or value exists for
2777db96d56Sopenharmony_ci   *name*.  It returns a list consisting of one item if only one such value exists.
2787db96d56Sopenharmony_ci
2797db96d56Sopenharmony_ciUsing these methods you can write nice compact code::
2807db96d56Sopenharmony_ci
2817db96d56Sopenharmony_ci   import cgi
2827db96d56Sopenharmony_ci   form = cgi.FieldStorage()
2837db96d56Sopenharmony_ci   user = form.getfirst("user", "").upper()    # This way it's safe.
2847db96d56Sopenharmony_ci   for item in form.getlist("item"):
2857db96d56Sopenharmony_ci       do_something(item)
2867db96d56Sopenharmony_ci
2877db96d56Sopenharmony_ci
2887db96d56Sopenharmony_ci.. _functions-in-cgi-module:
2897db96d56Sopenharmony_ci
2907db96d56Sopenharmony_ciFunctions
2917db96d56Sopenharmony_ci---------
2927db96d56Sopenharmony_ci
2937db96d56Sopenharmony_ciThese are useful if you want more control, or if you want to employ some of the
2947db96d56Sopenharmony_cialgorithms implemented in this module in other circumstances.
2957db96d56Sopenharmony_ci
2967db96d56Sopenharmony_ci
2977db96d56Sopenharmony_ci.. function:: parse(fp=None, environ=os.environ, keep_blank_values=False, strict_parsing=False, separator="&")
2987db96d56Sopenharmony_ci
2997db96d56Sopenharmony_ci   Parse a query in the environment or from a file (the file defaults to
3007db96d56Sopenharmony_ci   ``sys.stdin``).  The *keep_blank_values*, *strict_parsing* and *separator* parameters are
3017db96d56Sopenharmony_ci   passed to :func:`urllib.parse.parse_qs` unchanged.
3027db96d56Sopenharmony_ci
3037db96d56Sopenharmony_ci   .. deprecated-removed:: 3.11 3.13
3047db96d56Sopenharmony_ci      This function, like the rest of the :mod:`cgi` module, is deprecated.
3057db96d56Sopenharmony_ci      It can be replaced by calling :func:`urllib.parse.parse_qs` directly
3067db96d56Sopenharmony_ci      on the desired query string (except for ``multipart/form-data`` input,
3077db96d56Sopenharmony_ci      which can be handled as described for :func:`parse_multipart`).
3087db96d56Sopenharmony_ci
3097db96d56Sopenharmony_ci
3107db96d56Sopenharmony_ci.. function:: parse_multipart(fp, pdict, encoding="utf-8", errors="replace", separator="&")
3117db96d56Sopenharmony_ci
3127db96d56Sopenharmony_ci   Parse input of type :mimetype:`multipart/form-data` (for  file uploads).
3137db96d56Sopenharmony_ci   Arguments are *fp* for the input file, *pdict* for a dictionary containing
3147db96d56Sopenharmony_ci   other parameters in the :mailheader:`Content-Type` header, and *encoding*,
3157db96d56Sopenharmony_ci   the request encoding.
3167db96d56Sopenharmony_ci
3177db96d56Sopenharmony_ci   Returns a dictionary just like :func:`urllib.parse.parse_qs`: keys are the
3187db96d56Sopenharmony_ci   field names, each value is a list of values for that field. For non-file
3197db96d56Sopenharmony_ci   fields, the value is a list of strings.
3207db96d56Sopenharmony_ci
3217db96d56Sopenharmony_ci   This is easy to use but not much good if you are expecting megabytes to be
3227db96d56Sopenharmony_ci   uploaded --- in that case, use the :class:`FieldStorage` class instead
3237db96d56Sopenharmony_ci   which is much more flexible.
3247db96d56Sopenharmony_ci
3257db96d56Sopenharmony_ci   .. versionchanged:: 3.7
3267db96d56Sopenharmony_ci      Added the *encoding* and *errors* parameters.  For non-file fields, the
3277db96d56Sopenharmony_ci      value is now a list of strings, not bytes.
3287db96d56Sopenharmony_ci
3297db96d56Sopenharmony_ci   .. versionchanged:: 3.10
3307db96d56Sopenharmony_ci      Added the *separator* parameter.
3317db96d56Sopenharmony_ci
3327db96d56Sopenharmony_ci   .. deprecated-removed:: 3.11 3.13
3337db96d56Sopenharmony_ci      This function, like the rest of the :mod:`cgi` module, is deprecated.
3347db96d56Sopenharmony_ci      It can be replaced with the functionality in the :mod:`email` package
3357db96d56Sopenharmony_ci      (e.g. :class:`email.message.EmailMessage`/:class:`email.message.Message`)
3367db96d56Sopenharmony_ci      which implements the same MIME RFCs, or with the
3377db96d56Sopenharmony_ci      `multipart <https://pypi.org/project/multipart/>`__ PyPI project.
3387db96d56Sopenharmony_ci
3397db96d56Sopenharmony_ci
3407db96d56Sopenharmony_ci.. function:: parse_header(string)
3417db96d56Sopenharmony_ci
3427db96d56Sopenharmony_ci   Parse a MIME header (such as :mailheader:`Content-Type`) into a main value and a
3437db96d56Sopenharmony_ci   dictionary of parameters.
3447db96d56Sopenharmony_ci
3457db96d56Sopenharmony_ci   .. deprecated-removed:: 3.11 3.13
3467db96d56Sopenharmony_ci      This function, like the rest of the :mod:`cgi` module, is deprecated.
3477db96d56Sopenharmony_ci      It can be replaced with the functionality in the :mod:`email` package,
3487db96d56Sopenharmony_ci      which implements the same MIME RFCs.
3497db96d56Sopenharmony_ci
3507db96d56Sopenharmony_ci      For example, with :class:`email.message.EmailMessage`::
3517db96d56Sopenharmony_ci
3527db96d56Sopenharmony_ci          from email.message import EmailMessage
3537db96d56Sopenharmony_ci          msg = EmailMessage()
3547db96d56Sopenharmony_ci          msg['content-type'] = 'application/json; charset="utf8"'
3557db96d56Sopenharmony_ci          main, params = msg.get_content_type(), msg['content-type'].params
3567db96d56Sopenharmony_ci
3577db96d56Sopenharmony_ci
3587db96d56Sopenharmony_ci.. function:: test()
3597db96d56Sopenharmony_ci
3607db96d56Sopenharmony_ci   Robust test CGI script, usable as main program. Writes minimal HTTP headers and
3617db96d56Sopenharmony_ci   formats all information provided to the script in HTML format.
3627db96d56Sopenharmony_ci
3637db96d56Sopenharmony_ci
3647db96d56Sopenharmony_ci.. function:: print_environ()
3657db96d56Sopenharmony_ci
3667db96d56Sopenharmony_ci   Format the shell environment in HTML.
3677db96d56Sopenharmony_ci
3687db96d56Sopenharmony_ci
3697db96d56Sopenharmony_ci.. function:: print_form(form)
3707db96d56Sopenharmony_ci
3717db96d56Sopenharmony_ci   Format a form in HTML.
3727db96d56Sopenharmony_ci
3737db96d56Sopenharmony_ci
3747db96d56Sopenharmony_ci.. function:: print_directory()
3757db96d56Sopenharmony_ci
3767db96d56Sopenharmony_ci   Format the current directory in HTML.
3777db96d56Sopenharmony_ci
3787db96d56Sopenharmony_ci
3797db96d56Sopenharmony_ci.. function:: print_environ_usage()
3807db96d56Sopenharmony_ci
3817db96d56Sopenharmony_ci   Print a list of useful (used by CGI) environment variables in HTML.
3827db96d56Sopenharmony_ci
3837db96d56Sopenharmony_ci
3847db96d56Sopenharmony_ci.. _cgi-security:
3857db96d56Sopenharmony_ci
3867db96d56Sopenharmony_ciCaring about security
3877db96d56Sopenharmony_ci---------------------
3887db96d56Sopenharmony_ci
3897db96d56Sopenharmony_ci.. index:: pair: CGI; security
3907db96d56Sopenharmony_ci
3917db96d56Sopenharmony_ciThere's one important rule: if you invoke an external program (via
3927db96d56Sopenharmony_ci:func:`os.system`, :func:`os.popen` or other functions with similar
3937db96d56Sopenharmony_cifunctionality), make very sure you don't pass arbitrary strings received from
3947db96d56Sopenharmony_cithe client to the shell.  This is a well-known security hole whereby clever
3957db96d56Sopenharmony_cihackers anywhere on the web can exploit a gullible CGI script to invoke
3967db96d56Sopenharmony_ciarbitrary shell commands.  Even parts of the URL or field names cannot be
3977db96d56Sopenharmony_citrusted, since the request doesn't have to come from your form!
3987db96d56Sopenharmony_ci
3997db96d56Sopenharmony_ciTo be on the safe side, if you must pass a string gotten from a form to a shell
4007db96d56Sopenharmony_cicommand, you should make sure the string contains only alphanumeric characters,
4017db96d56Sopenharmony_cidashes, underscores, and periods.
4027db96d56Sopenharmony_ci
4037db96d56Sopenharmony_ci
4047db96d56Sopenharmony_ciInstalling your CGI script on a Unix system
4057db96d56Sopenharmony_ci-------------------------------------------
4067db96d56Sopenharmony_ci
4077db96d56Sopenharmony_ciRead the documentation for your HTTP server and check with your local system
4087db96d56Sopenharmony_ciadministrator to find the directory where CGI scripts should be installed;
4097db96d56Sopenharmony_ciusually this is in a directory :file:`cgi-bin` in the server tree.
4107db96d56Sopenharmony_ci
4117db96d56Sopenharmony_ciMake sure that your script is readable and executable by "others"; the Unix file
4127db96d56Sopenharmony_cimode should be ``0o755`` octal (use ``chmod 0755 filename``).  Make sure that the
4137db96d56Sopenharmony_cifirst line of the script contains ``#!`` starting in column 1 followed by the
4147db96d56Sopenharmony_cipathname of the Python interpreter, for instance::
4157db96d56Sopenharmony_ci
4167db96d56Sopenharmony_ci   #!/usr/local/bin/python
4177db96d56Sopenharmony_ci
4187db96d56Sopenharmony_ciMake sure the Python interpreter exists and is executable by "others".
4197db96d56Sopenharmony_ci
4207db96d56Sopenharmony_ciMake sure that any files your script needs to read or write are readable or
4217db96d56Sopenharmony_ciwritable, respectively, by "others" --- their mode should be ``0o644`` for
4227db96d56Sopenharmony_cireadable and ``0o666`` for writable.  This is because, for security reasons, the
4237db96d56Sopenharmony_ciHTTP server executes your script as user "nobody", without any special
4247db96d56Sopenharmony_ciprivileges.  It can only read (write, execute) files that everybody can read
4257db96d56Sopenharmony_ci(write, execute).  The current directory at execution time is also different (it
4267db96d56Sopenharmony_ciis usually the server's cgi-bin directory) and the set of environment variables
4277db96d56Sopenharmony_ciis also different from what you get when you log in.  In particular, don't count
4287db96d56Sopenharmony_cion the shell's search path for executables (:envvar:`PATH`) or the Python module
4297db96d56Sopenharmony_cisearch path (:envvar:`PYTHONPATH`) to be set to anything interesting.
4307db96d56Sopenharmony_ci
4317db96d56Sopenharmony_ciIf you need to load modules from a directory which is not on Python's default
4327db96d56Sopenharmony_cimodule search path, you can change the path in your script, before importing
4337db96d56Sopenharmony_ciother modules.  For example::
4347db96d56Sopenharmony_ci
4357db96d56Sopenharmony_ci   import sys
4367db96d56Sopenharmony_ci   sys.path.insert(0, "/usr/home/joe/lib/python")
4377db96d56Sopenharmony_ci   sys.path.insert(0, "/usr/local/lib/python")
4387db96d56Sopenharmony_ci
4397db96d56Sopenharmony_ci(This way, the directory inserted last will be searched first!)
4407db96d56Sopenharmony_ci
4417db96d56Sopenharmony_ciInstructions for non-Unix systems will vary; check your HTTP server's
4427db96d56Sopenharmony_cidocumentation (it will usually have a section on CGI scripts).
4437db96d56Sopenharmony_ci
4447db96d56Sopenharmony_ci
4457db96d56Sopenharmony_ciTesting your CGI script
4467db96d56Sopenharmony_ci-----------------------
4477db96d56Sopenharmony_ci
4487db96d56Sopenharmony_ciUnfortunately, a CGI script will generally not run when you try it from the
4497db96d56Sopenharmony_cicommand line, and a script that works perfectly from the command line may fail
4507db96d56Sopenharmony_cimysteriously when run from the server.  There's one reason why you should still
4517db96d56Sopenharmony_citest your script from the command line: if it contains a syntax error, the
4527db96d56Sopenharmony_ciPython interpreter won't execute it at all, and the HTTP server will most likely
4537db96d56Sopenharmony_cisend a cryptic error to the client.
4547db96d56Sopenharmony_ci
4557db96d56Sopenharmony_ciAssuming your script has no syntax errors, yet it does not work, you have no
4567db96d56Sopenharmony_cichoice but to read the next section.
4577db96d56Sopenharmony_ci
4587db96d56Sopenharmony_ci
4597db96d56Sopenharmony_ciDebugging CGI scripts
4607db96d56Sopenharmony_ci---------------------
4617db96d56Sopenharmony_ci
4627db96d56Sopenharmony_ci.. index:: pair: CGI; debugging
4637db96d56Sopenharmony_ci
4647db96d56Sopenharmony_ciFirst of all, check for trivial installation errors --- reading the section
4657db96d56Sopenharmony_ciabove on installing your CGI script carefully can save you a lot of time.  If
4667db96d56Sopenharmony_ciyou wonder whether you have understood the installation procedure correctly, try
4677db96d56Sopenharmony_ciinstalling a copy of this module file (:file:`cgi.py`) as a CGI script.  When
4687db96d56Sopenharmony_ciinvoked as a script, the file will dump its environment and the contents of the
4697db96d56Sopenharmony_ciform in HTML format. Give it the right mode etc., and send it a request.  If it's
4707db96d56Sopenharmony_ciinstalled in the standard :file:`cgi-bin` directory, it should be possible to
4717db96d56Sopenharmony_cisend it a request by entering a URL into your browser of the form:
4727db96d56Sopenharmony_ci
4737db96d56Sopenharmony_ci.. code-block:: none
4747db96d56Sopenharmony_ci
4757db96d56Sopenharmony_ci   http://yourhostname/cgi-bin/cgi.py?name=Joe+Blow&addr=At+Home
4767db96d56Sopenharmony_ci
4777db96d56Sopenharmony_ciIf this gives an error of type 404, the server cannot find the script -- perhaps
4787db96d56Sopenharmony_ciyou need to install it in a different directory.  If it gives another error,
4797db96d56Sopenharmony_cithere's an installation problem that you should fix before trying to go any
4807db96d56Sopenharmony_cifurther.  If you get a nicely formatted listing of the environment and form
4817db96d56Sopenharmony_cicontent (in this example, the fields should be listed as "addr" with value "At
4827db96d56Sopenharmony_ciHome" and "name" with value "Joe Blow"), the :file:`cgi.py` script has been
4837db96d56Sopenharmony_ciinstalled correctly.  If you follow the same procedure for your own script, you
4847db96d56Sopenharmony_cishould now be able to debug it.
4857db96d56Sopenharmony_ci
4867db96d56Sopenharmony_ciThe next step could be to call the :mod:`cgi` module's :func:`test` function
4877db96d56Sopenharmony_cifrom your script: replace its main code with the single statement ::
4887db96d56Sopenharmony_ci
4897db96d56Sopenharmony_ci   cgi.test()
4907db96d56Sopenharmony_ci
4917db96d56Sopenharmony_ciThis should produce the same results as those gotten from installing the
4927db96d56Sopenharmony_ci:file:`cgi.py` file itself.
4937db96d56Sopenharmony_ci
4947db96d56Sopenharmony_ciWhen an ordinary Python script raises an unhandled exception (for whatever
4957db96d56Sopenharmony_cireason: of a typo in a module name, a file that can't be opened, etc.), the
4967db96d56Sopenharmony_ciPython interpreter prints a nice traceback and exits.  While the Python
4977db96d56Sopenharmony_ciinterpreter will still do this when your CGI script raises an exception, most
4987db96d56Sopenharmony_cilikely the traceback will end up in one of the HTTP server's log files, or be
4997db96d56Sopenharmony_cidiscarded altogether.
5007db96d56Sopenharmony_ci
5017db96d56Sopenharmony_ciFortunately, once you have managed to get your script to execute *some* code,
5027db96d56Sopenharmony_ciyou can easily send tracebacks to the web browser using the :mod:`cgitb` module.
5037db96d56Sopenharmony_ciIf you haven't done so already, just add the lines::
5047db96d56Sopenharmony_ci
5057db96d56Sopenharmony_ci   import cgitb
5067db96d56Sopenharmony_ci   cgitb.enable()
5077db96d56Sopenharmony_ci
5087db96d56Sopenharmony_cito the top of your script.  Then try running it again; when a problem occurs,
5097db96d56Sopenharmony_ciyou should see a detailed report that will likely make apparent the cause of the
5107db96d56Sopenharmony_cicrash.
5117db96d56Sopenharmony_ci
5127db96d56Sopenharmony_ciIf you suspect that there may be a problem in importing the :mod:`cgitb` module,
5137db96d56Sopenharmony_ciyou can use an even more robust approach (which only uses built-in modules)::
5147db96d56Sopenharmony_ci
5157db96d56Sopenharmony_ci   import sys
5167db96d56Sopenharmony_ci   sys.stderr = sys.stdout
5177db96d56Sopenharmony_ci   print("Content-Type: text/plain")
5187db96d56Sopenharmony_ci   print()
5197db96d56Sopenharmony_ci   ...your code here...
5207db96d56Sopenharmony_ci
5217db96d56Sopenharmony_ciThis relies on the Python interpreter to print the traceback.  The content type
5227db96d56Sopenharmony_ciof the output is set to plain text, which disables all HTML processing.  If your
5237db96d56Sopenharmony_ciscript works, the raw HTML will be displayed by your client.  If it raises an
5247db96d56Sopenharmony_ciexception, most likely after the first two lines have been printed, a traceback
5257db96d56Sopenharmony_ciwill be displayed. Because no HTML interpretation is going on, the traceback
5267db96d56Sopenharmony_ciwill be readable.
5277db96d56Sopenharmony_ci
5287db96d56Sopenharmony_ci
5297db96d56Sopenharmony_ciCommon problems and solutions
5307db96d56Sopenharmony_ci-----------------------------
5317db96d56Sopenharmony_ci
5327db96d56Sopenharmony_ci* Most HTTP servers buffer the output from CGI scripts until the script is
5337db96d56Sopenharmony_ci  completed.  This means that it is not possible to display a progress report on
5347db96d56Sopenharmony_ci  the client's display while the script is running.
5357db96d56Sopenharmony_ci
5367db96d56Sopenharmony_ci* Check the installation instructions above.
5377db96d56Sopenharmony_ci
5387db96d56Sopenharmony_ci* Check the HTTP server's log files.  (``tail -f logfile`` in a separate window
5397db96d56Sopenharmony_ci  may be useful!)
5407db96d56Sopenharmony_ci
5417db96d56Sopenharmony_ci* Always check a script for syntax errors first, by doing something like
5427db96d56Sopenharmony_ci  ``python script.py``.
5437db96d56Sopenharmony_ci
5447db96d56Sopenharmony_ci* If your script does not have any syntax errors, try adding ``import cgitb;
5457db96d56Sopenharmony_ci  cgitb.enable()`` to the top of the script.
5467db96d56Sopenharmony_ci
5477db96d56Sopenharmony_ci* When invoking external programs, make sure they can be found. Usually, this
5487db96d56Sopenharmony_ci  means using absolute path names --- :envvar:`PATH` is usually not set to a very
5497db96d56Sopenharmony_ci  useful value in a CGI script.
5507db96d56Sopenharmony_ci
5517db96d56Sopenharmony_ci* When reading or writing external files, make sure they can be read or written
5527db96d56Sopenharmony_ci  by the userid under which your CGI script will be running: this is typically the
5537db96d56Sopenharmony_ci  userid under which the web server is running, or some explicitly specified
5547db96d56Sopenharmony_ci  userid for a web server's ``suexec`` feature.
5557db96d56Sopenharmony_ci
5567db96d56Sopenharmony_ci* Don't try to give a CGI script a set-uid mode.  This doesn't work on most
5577db96d56Sopenharmony_ci  systems, and is a security liability as well.
5587db96d56Sopenharmony_ci
5597db96d56Sopenharmony_ci.. rubric:: Footnotes
5607db96d56Sopenharmony_ci
5617db96d56Sopenharmony_ci.. [#] Note that some recent versions of the HTML specification do state what
5627db96d56Sopenharmony_ci   order the field values should be supplied in, but knowing whether a request
5637db96d56Sopenharmony_ci   was received from a conforming browser, or even from a browser at all, is
5647db96d56Sopenharmony_ci   tedious and error-prone.
565