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

Simon Willison cs1spw at bath.ac.uk
Thu Oct 23 17:56:09 EDT 2003


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

Output:

Content-Type: text/html
Set-Cookie: name=Simon
X-Additional-Header: Another header
Content-Length: 30

<html><h1>Hi there</h1></html>
<form method="post"><input type="text" name="name"></form>

Cheers,

Simon Willison
http://simon.incutio.com/




More information about the Web-SIG mailing list