This PEP proposes a set of enhancements to the CGI developmentfacilities in the Python standard library. Enhancements might benew features, new modules for tasks such as cookie support, orremoval 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 futurePython release.
This section lists changes that have been suggested, but aboutwhich no firm decision has yet been made. In the final version ofthis PEP, this section should be empty, as all the changes shouldbe classified as accepted or rejected.
cgi.py: We should not be told to create our own subclass just sowe can handle file uploads. As a practical matter, I have yet tofind the time to do this right, so I end up reading cgi.py’s tempfile into, at best, another file. Some of our legacy code actuallyreads it into a second temp file, then into a final destination!And even if we did, that would mean creating yet another objectwith its__init__ call and associated overhead.
cgi.py: Currently, query data with no= are ignored. Even ifkeep_blank_values is set, queries like...?value=&... arereturned with blank values but queries like...?value&... arecompletely lost. It would be great if such data were madeavailable through theFieldStorage interface, either as entrieswithNone as values, or in a separate list.
Utility function: build a query string from a list of 2-tuples
Dictionary-related utility classes:NoKeyErrors (returns an emptystring, never aKeyError),PartialStringSubstitution (returnsthe original key string, never aKeyError)
This section lists details about entire new packages or modulesthat should be added to the Python standard library.
This section lists details of major changes to existing modules,whether in implementation or in interface. The changes in thissection therefore carry greater degrees of risk, either inintroducing bugs or a backward incompatibility.
The cgi.py module would be deprecated. (XXX A new module orpackage name hasn’t been chosen yet: ‘web’? ‘cgilib’?)
This section lists details of minor changes to existing modules.These changes should have relatively small implementations, andhave little risk of introducing incompatibilities with previousversions.
The changes listed in this section were proposed for Python 2.1,but were rejected as unsuitable. For each rejected change, arationale is given describing why the change was deemedinappropriate.
XXX open issues: naming convention (studlycaps orunderline-separated?); need to look at thecgi.parse*() functionsand see if they can be simplified, too.
Parsing functions: carry over most of theparse* functions fromcgi.py
# The Response class borrows most of its methods from Zope's# HTTPResponse class.classResponse:""" Attributes: status: HTTP status code to return headers: dictionary of response headers body: string containing the body of the HTTP response """def__init__(self,status=200,headers={},body=""):passdefsetStatus(self,status,reason=None):"Set the numeric HTTP response code"passdefsetHeader(self,name,value):"Set an HTTP header"passdefsetBody(self,body):"Set the body of the response"passdefsetCookie(self,name,value,path='/',comment=None,domain=None,max-age=None,expires=None,secure=0):"Set a cookie"passdefexpireCookie(self,name):"Remove a cookie from the user"passdefredirect(self,url):"Redirect the browser to another URL"passdef__str__(self):"Convert entire response to a string"passdefdump(self):"Return a string representation useful for debugging"pass# XXX methods for specific classes of error:serverError,# badRequest, etc.?classRequest:""" Attributes: XXX should these be dictionaries, or dictionary-like objects? .headers : dictionary containing HTTP headers .cookies : dictionary of cookies .fields : data from the form .env : environment dictionary """def__init__(self,environ=os.environ,stdin=sys.stdin,keep_blank_values=1,strict_parsing=0):"""Initialize the request object, using the provided environment and standard input."""pass# Should people just use the dictionaries directly?defgetHeader(self,name,default=None):passdefgetCookie(self,name,default=None):passdefgetField(self,name,default=None):"Return field's value as a string (even if it's an uploaded file)"passdefgetUploadedFile(self,name):"""Returns a file object that can be read to obtain the contents of an uploaded file. XXX should this report an error if the field isn't actually an uploaded file? Or should it wrap a StringIO around simple fields for consistency? """defgetURL(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 "http://foo.com/bar/baz/quux", n=2 would return "http://foo.com/bar". Does not include the query string (if any) """defgetBaseURL(self,n=0):"""Return the base URL of the current request, adding 'n' path components to the end to recreate more of the whole URL. 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". Returned URL does not include the query string, if any. """defdump(self):"String representation suitable for debugging output"pass# Possibilities? I don't know if these are worth doing in the# basic objects.defgetBrowser(self):"Returns Mozilla/IE/Lynx/Opera/whatever"defisSecure(self):"Return true if this is an SSLified request"# Module-level functiondefwrapper(func,logfile=sys.stderr):""" Calls the function 'func', passing it the arguments (request, response, logfile). Exceptions are trapped and sent to the file 'logfile'. """# This wrapper will detect if it's being called from the command-line,# and if so, it will run in a debugging mode; name=value pairs# can be entered on standard input to set field values.# (XXX how to do file uploads in this syntax?)
This document has been placed in the public domain.
Source:https://github.com/python/peps/blob/main/peps/pep-0222.rst
Last modified:2025-02-01 08:59:27 GMT