2000-08-18 11:21:45 -04:00
|
|
|
PEP: 222
|
|
|
|
Title: Web Library Enhancements
|
|
|
|
Version: $Revision$
|
2006-03-23 15:13:19 -05:00
|
|
|
Last-Modified: $Date$
|
2002-10-30 20:35:08 -05:00
|
|
|
Author: A.M. Kuchling <amk@amk.ca>
|
2001-01-16 22:44:14 -05:00
|
|
|
Status: Deferred
|
2000-08-18 11:21:45 -04:00
|
|
|
Type: Standards Track
|
2017-02-10 14:05:40 -05:00
|
|
|
Content-Type: text/x-rst
|
2000-08-18 11:21:45 -04:00
|
|
|
Created: 18-Aug-2000
|
2007-06-19 00:20:07 -04:00
|
|
|
Python-Version: 2.1
|
2000-12-22 15:35:20 -05:00
|
|
|
Post-History: 22-Dec-2000
|
2000-08-18 11:21:45 -04:00
|
|
|
|
|
|
|
|
|
|
|
Abstract
|
2017-02-10 14:05:40 -05:00
|
|
|
========
|
2000-08-18 11:21:45 -04:00
|
|
|
|
2017-02-10 14:05:40 -05:00
|
|
|
This PEP proposes a set of enhancements to the CGI development
|
|
|
|
facilities in the Python standard library. Enhancements might be
|
|
|
|
new features, new modules for tasks such as cookie support, or
|
|
|
|
removal of obsolete code.
|
|
|
|
|
|
|
|
The original intent was to make improvements to Python 2.1.
|
|
|
|
However, there seemed little interest from the Python community,
|
|
|
|
and time was lacking, so this PEP has been deferred to some future
|
|
|
|
Python release.
|
2000-08-18 11:21:45 -04:00
|
|
|
|
2000-10-28 20:16:37 -04:00
|
|
|
|
2000-12-22 15:35:20 -05:00
|
|
|
Open Issues
|
2017-02-10 14:05:40 -05:00
|
|
|
===========
|
|
|
|
|
|
|
|
This section lists changes that have been suggested, but about
|
|
|
|
which no firm decision has yet been made. In the final version of
|
|
|
|
this PEP, this section should be empty, as all the changes should
|
|
|
|
be classified as accepted or rejected.
|
2000-10-28 20:16:37 -04:00
|
|
|
|
2017-02-10 14:05:40 -05:00
|
|
|
cgi.py: We should not be told to create our own subclass just so
|
|
|
|
we can handle file uploads. As a practical matter, I have yet to
|
|
|
|
find the time to do this right, so I end up reading cgi.py's temp
|
|
|
|
file into, at best, another file. Some of our legacy code actually
|
|
|
|
reads it into a second temp file, then into a final destination!
|
|
|
|
And even if we did, that would mean creating yet another object
|
|
|
|
with its ``__init__`` call and associated overhead.
|
2000-10-28 20:16:37 -04:00
|
|
|
|
2017-02-10 14:05:40 -05:00
|
|
|
cgi.py: Currently, query data with no ``=`` are ignored. Even if
|
2017-04-05 12:14:26 -04:00
|
|
|
``keep_blank_values`` is set, queries like ``...?value=&...`` are
|
2017-02-10 14:05:40 -05:00
|
|
|
returned with blank values but queries like ``...?value&...`` are
|
|
|
|
completely lost. It would be great if such data were made
|
|
|
|
available through the ``FieldStorage`` interface, either as entries
|
2017-04-05 12:14:26 -04:00
|
|
|
with ``None`` as values, or in a separate list.
|
2000-10-28 20:16:37 -04:00
|
|
|
|
2017-02-10 14:05:40 -05:00
|
|
|
Utility function: build a query string from a list of 2-tuples
|
2000-10-28 20:16:37 -04:00
|
|
|
|
2017-02-10 14:05:40 -05:00
|
|
|
Dictionary-related utility classes: ``NoKeyErrors`` (returns an empty
|
|
|
|
string, never a ``KeyError``), ``PartialStringSubstitution`` (returns
|
|
|
|
the original key string, never a ``KeyError``)
|
2000-11-10 12:06:53 -05:00
|
|
|
|
2000-10-28 20:16:37 -04:00
|
|
|
|
|
|
|
|
|
|
|
New Modules
|
2017-02-10 14:05:40 -05:00
|
|
|
===========
|
|
|
|
|
|
|
|
This section lists details about entire new packages or modules
|
|
|
|
that should be added to the Python standard library.
|
2000-10-28 20:16:37 -04:00
|
|
|
|
2017-02-10 14:05:40 -05:00
|
|
|
* fcgi.py : A new module adding support for the FastCGI protocol.
|
|
|
|
Robin Dunn's code needs to be ported to Windows, though.
|
2000-10-28 20:16:37 -04:00
|
|
|
|
|
|
|
|
|
|
|
Major Changes to Existing Modules
|
2017-02-10 14:05:40 -05:00
|
|
|
=================================
|
2000-10-28 20:16:37 -04:00
|
|
|
|
2017-02-10 14:05:40 -05:00
|
|
|
This section lists details of major changes to existing modules,
|
|
|
|
whether in implementation or in interface. The changes in this
|
|
|
|
section therefore carry greater degrees of risk, either in
|
|
|
|
introducing bugs or a backward incompatibility.
|
|
|
|
|
|
|
|
The cgi.py module would be deprecated. (XXX A new module or
|
|
|
|
package name hasn't been chosen yet: 'web'? 'cgilib'?)
|
2000-12-22 15:35:20 -05:00
|
|
|
|
2000-10-28 20:16:37 -04:00
|
|
|
|
|
|
|
Minor Changes to Existing Modules
|
2017-02-10 14:05:40 -05:00
|
|
|
=================================
|
2000-10-28 20:16:37 -04:00
|
|
|
|
2017-02-10 14:05:40 -05:00
|
|
|
This section lists details of minor changes to existing modules.
|
|
|
|
These changes should have relatively small implementations, and
|
|
|
|
have little risk of introducing incompatibilities with previous
|
|
|
|
versions.
|
2000-10-28 20:16:37 -04:00
|
|
|
|
|
|
|
|
|
|
|
Rejected Changes
|
2017-02-10 14:05:40 -05:00
|
|
|
================
|
|
|
|
|
|
|
|
The changes listed in this section were proposed for Python 2.1,
|
|
|
|
but were rejected as unsuitable. For each rejected change, a
|
|
|
|
rationale is given describing why the change was deemed
|
|
|
|
inappropriate.
|
2000-10-28 20:16:37 -04:00
|
|
|
|
2017-02-10 14:05:40 -05:00
|
|
|
* An HTML generation module is not part of this PEP. Several such
|
|
|
|
modules exist, ranging from HTMLgen's purely programming
|
|
|
|
interface to ASP-inspired simple templating to DTML's complex
|
|
|
|
templating. There's no indication of which templating module to
|
|
|
|
enshrine in the standard library, and that probably means that
|
|
|
|
no module should be so chosen.
|
2000-10-28 20:16:37 -04:00
|
|
|
|
2017-02-10 14:05:40 -05:00
|
|
|
* cgi.py: Allowing a combination of query data and POST data.
|
|
|
|
This doesn't seem to be standard at all, and therefore is
|
|
|
|
dubious practice.
|
2000-10-28 20:16:37 -04:00
|
|
|
|
|
|
|
|
2000-12-12 12:34:39 -05:00
|
|
|
Proposed Interface
|
2017-02-10 14:05:40 -05:00
|
|
|
==================
|
2000-12-12 12:34:39 -05:00
|
|
|
|
2017-02-10 14:05:40 -05:00
|
|
|
XXX open issues: naming convention (studlycaps or
|
|
|
|
underline-separated?); need to look at the ``cgi.parse*()`` functions
|
|
|
|
and see if they can be simplified, too.
|
|
|
|
|
|
|
|
Parsing functions: carry over most of the ``parse*`` functions from
|
|
|
|
cgi.py
|
|
|
|
|
|
|
|
::
|
2000-12-17 22:01:23 -05:00
|
|
|
|
2000-12-12 12:34:39 -05:00
|
|
|
# The Response class borrows most of its methods from Zope's
|
|
|
|
# HTTPResponse class.
|
2017-02-10 14:05:40 -05:00
|
|
|
|
2000-12-12 12:34:39 -05:00
|
|
|
class Response:
|
|
|
|
"""
|
|
|
|
Attributes:
|
|
|
|
status: HTTP status code to return
|
|
|
|
headers: dictionary of response headers
|
|
|
|
body: string containing the body of the HTTP response
|
|
|
|
"""
|
2017-02-10 14:05:40 -05:00
|
|
|
|
2000-12-12 12:34:39 -05:00
|
|
|
def __init__(self, status=200, headers={}, body=""):
|
|
|
|
pass
|
2017-02-10 14:05:40 -05:00
|
|
|
|
2000-12-12 12:34:39 -05:00
|
|
|
def setStatus(self, status, reason=None):
|
|
|
|
"Set the numeric HTTP response code"
|
|
|
|
pass
|
2017-02-10 14:05:40 -05:00
|
|
|
|
2000-12-12 12:34:39 -05:00
|
|
|
def setHeader(self, name, value):
|
|
|
|
"Set an HTTP header"
|
|
|
|
pass
|
2017-02-10 14:05:40 -05:00
|
|
|
|
2000-12-12 12:34:39 -05:00
|
|
|
def setBody(self, body):
|
|
|
|
"Set the body of the response"
|
|
|
|
pass
|
2017-02-10 14:05:40 -05:00
|
|
|
|
2000-12-12 12:34:39 -05:00
|
|
|
def setCookie(self, name, value,
|
2017-02-10 14:05:40 -05:00
|
|
|
path = '/',
|
|
|
|
comment = None,
|
|
|
|
domain = None,
|
2000-12-22 15:35:20 -05:00
|
|
|
max-age = None,
|
|
|
|
expires = None,
|
|
|
|
secure = 0
|
2000-12-12 12:34:39 -05:00
|
|
|
):
|
|
|
|
"Set a cookie"
|
|
|
|
pass
|
2017-02-10 14:05:40 -05:00
|
|
|
|
2000-12-12 12:34:39 -05:00
|
|
|
def expireCookie(self, name):
|
|
|
|
"Remove a cookie from the user"
|
|
|
|
pass
|
2017-02-10 14:05:40 -05:00
|
|
|
|
2000-12-12 12:34:39 -05:00
|
|
|
def redirect(self, url):
|
|
|
|
"Redirect the browser to another URL"
|
|
|
|
pass
|
2017-02-10 14:05:40 -05:00
|
|
|
|
2000-12-12 12:34:39 -05:00
|
|
|
def __str__(self):
|
|
|
|
"Convert entire response to a string"
|
|
|
|
pass
|
2017-02-10 14:05:40 -05:00
|
|
|
|
2000-12-12 12:34:39 -05:00
|
|
|
def dump(self):
|
|
|
|
"Return a string representation useful for debugging"
|
|
|
|
pass
|
2017-02-10 14:05:40 -05:00
|
|
|
|
|
|
|
# XXX methods for specific classes of error:serverError,
|
2000-12-22 15:35:20 -05:00
|
|
|
# badRequest, etc.?
|
2017-02-10 14:05:40 -05:00
|
|
|
|
|
|
|
|
2000-12-12 12:34:39 -05:00
|
|
|
class Request:
|
2017-02-10 14:05:40 -05:00
|
|
|
|
2000-12-12 12:34:39 -05:00
|
|
|
"""
|
2017-02-10 14:05:40 -05:00
|
|
|
Attributes:
|
2000-12-22 15:35:20 -05:00
|
|
|
|
|
|
|
XXX should these be dictionaries, or dictionary-like objects?
|
2000-12-12 12:34:39 -05:00
|
|
|
.headers : dictionary containing HTTP headers
|
|
|
|
.cookies : dictionary of cookies
|
2000-12-22 15:35:20 -05:00
|
|
|
.fields : data from the form
|
2000-12-12 12:34:39 -05:00
|
|
|
.env : environment dictionary
|
|
|
|
"""
|
2017-02-10 14:05:40 -05:00
|
|
|
|
2000-12-12 12:34:39 -05:00
|
|
|
def __init__(self, environ=os.environ, stdin=sys.stdin,
|
2000-12-22 15:35:20 -05:00
|
|
|
keep_blank_values=1, strict_parsing=0):
|
2000-12-12 12:34:39 -05:00
|
|
|
"""Initialize the request object, using the provided environment
|
|
|
|
and standard input."""
|
|
|
|
pass
|
2017-02-10 14:05:40 -05:00
|
|
|
|
2000-12-12 12:34:39 -05:00
|
|
|
# Should people just use the dictionaries directly?
|
|
|
|
def getHeader(self, name, default=None):
|
|
|
|
pass
|
2017-02-10 14:05:40 -05:00
|
|
|
|
2000-12-12 12:34:39 -05:00
|
|
|
def getCookie(self, name, default=None):
|
|
|
|
pass
|
2017-02-10 14:05:40 -05:00
|
|
|
|
2000-12-12 12:34:39 -05:00
|
|
|
def getField(self, name, default=None):
|
2000-12-22 15:35:20 -05:00
|
|
|
"Return field's value as a string (even if it's an uploaded file)"
|
2000-12-12 12:34:39 -05:00
|
|
|
pass
|
2017-02-10 14:05:40 -05:00
|
|
|
|
2000-12-22 15:35:20 -05:00
|
|
|
def getUploadedFile(self, name):
|
|
|
|
"""Returns a file object that can be read to obtain the contents
|
2017-02-10 14:05:40 -05:00
|
|
|
of an uploaded file. XXX should this report an error if the
|
2000-12-22 15:35:20 -05:00
|
|
|
field isn't actually an uploaded file? Or should it wrap
|
|
|
|
a StringIO around simple fields for consistency?
|
|
|
|
"""
|
2017-02-10 14:05:40 -05:00
|
|
|
|
2000-12-12 12:34:39 -05:00
|
|
|
def getURL(self, n=0, query_string=0):
|
|
|
|
"""Return the URL of the current request, chopping off 'n' path
|
|
|
|
components from the right. Eg. if the URL is
|
2000-12-22 15:35:20 -05:00
|
|
|
"http://foo.com/bar/baz/quux", n=2 would return
|
2000-12-12 12:34:39 -05:00
|
|
|
"http://foo.com/bar". Does not include the query string (if
|
|
|
|
any)
|
|
|
|
"""
|
|
|
|
|
|
|
|
def getBaseURL(self, n=0):
|
|
|
|
"""Return the base URL of the current request, adding 'n' path
|
2017-02-10 14:05:40 -05:00
|
|
|
components to the end to recreate more of the whole URL.
|
|
|
|
|
2000-12-12 12:34:39 -05:00
|
|
|
Eg. if the request URL is
|
|
|
|
"http://foo.com/q/bar/baz/qux", n=0 would return
|
|
|
|
"http://foo.com/", and n=2 "http://foo.com/q/bar".
|
2017-02-10 14:05:40 -05:00
|
|
|
|
2000-12-12 12:34:39 -05:00
|
|
|
Returned URL does not include the query string, if any.
|
|
|
|
"""
|
2017-02-10 14:05:40 -05:00
|
|
|
|
2000-12-12 12:34:39 -05:00
|
|
|
def dump(self):
|
|
|
|
"String representation suitable for debugging output"
|
|
|
|
pass
|
2017-02-10 14:05:40 -05:00
|
|
|
|
|
|
|
# Possibilities? I don't know if these are worth doing in the
|
2000-12-22 15:35:20 -05:00
|
|
|
# basic objects.
|
2000-12-12 12:34:39 -05:00
|
|
|
def getBrowser(self):
|
|
|
|
"Returns Mozilla/IE/Lynx/Opera/whatever"
|
2017-02-10 14:05:40 -05:00
|
|
|
|
2000-12-12 12:34:39 -05:00
|
|
|
def isSecure(self):
|
|
|
|
"Return true if this is an SSLified request"
|
2000-12-22 15:35:20 -05:00
|
|
|
|
2017-02-10 14:05:40 -05:00
|
|
|
|
|
|
|
# Module-level function
|
2000-12-22 15:35:20 -05:00
|
|
|
def wrapper(func, logfile=sys.stderr):
|
2000-12-12 12:34:39 -05:00
|
|
|
"""
|
|
|
|
Calls the function 'func', passing it the arguments
|
|
|
|
(request, response, logfile). Exceptions are trapped and
|
2017-02-10 14:05:40 -05:00
|
|
|
sent to the file 'logfile'.
|
2000-12-12 12:34:39 -05:00
|
|
|
"""
|
2000-12-22 15:35:20 -05:00
|
|
|
# This wrapper will detect if it's being called from the command-line,
|
2017-02-10 14:05:40 -05:00
|
|
|
# and if so, it will run in a debugging mode; name=value pairs
|
2000-12-22 15:35:20 -05:00
|
|
|
# can be entered on standard input to set field values.
|
|
|
|
# (XXX how to do file uploads in this syntax?)
|
2000-12-12 12:34:39 -05:00
|
|
|
|
2017-02-10 14:05:40 -05:00
|
|
|
|
2000-12-12 12:34:39 -05:00
|
|
|
Copyright
|
2017-02-10 14:05:40 -05:00
|
|
|
=========
|
|
|
|
|
|
|
|
This document has been placed in the public domain.
|
|
|
|
|
2000-10-28 20:16:37 -04:00
|
|
|
|
|
|
|
|
2017-02-10 14:05:40 -05:00
|
|
|
..
|
|
|
|
Local Variables:
|
|
|
|
mode: indented-text
|
|
|
|
indent-tabs-mode: nil
|
|
|
|
End:
|