[Web-SIG] Defining a standard interface for common web tasks

David Fraser davidf at sjsoft.com
Fri Oct 24 03:38:49 EDT 2003


Simon Willison wrote:

> Bill Janssen wrote:
>
>>> Actually, I wrote an application using the cgi module this week - 
>>> it's just been deployed as the system to manage 
>>> http://coupons.lawrence.com/ :)
>>
>>
>> Sure, I write them all the time.  But what's missing?  What do you
>> have to work around?
>
>
> The biggest thing for me is distinguishing between GET and POST data. 
> Sending HTTP headers (including cookies) is also highly inconvenient 
> as with the cgi module they have to be manually constructed as HTTP 
> name:value pairs and sent before the rest of the text.
>
> This is where the request/response object model becomes very 
> attractive  - maybe something like the following:
>
> import web.cgi
>
> req = web.cgi.HTTPRequest() # Auto-populates with data from environment
> if req.POST:
>     # Form has been posted
>     body = 'Hi there, %s' % req.POST['name']
> else:
>     body = '<form method="post"><input type="text" name="name"></form>'
>
> res = web.cgi.HTTPResponse()
> res.content_type = 'text/html'
> res.set_cookie('name', 'Simon')
> res['X-Additional-Header'] = 'Another header'
> res.write('<html><h1>Hi there</h1>\n%s' % body)
> print res

For CGI, it would seem to make sense that you do something like the 
following:
res = web.cgi.HTTPResponse(sys.stdout)
res.content_type = 'text/html'
res.set_cookie('name', 'Simon')
res['X-Additional-Header'] = 'Another header'
res.send_headers()
res.write('<html><h1>Hi there</h1>\n%s' % body)

Then if you end up writing multiple parts, they can be output to stdout 
as they are written, rather than having to generate the entire response 
object first

David





More information about the Web-SIG mailing list