I’m having a bit of trouble getting the POST data (or anything from the request object) in my lab. I tried running this code in my program and in a separate file, but all I get is a segmentation fault (didn’t even know that was possible in Python). I’m pretty sure that the seg fault happens in the loop.
from cgi import escape from urllib import unquote# The Publisher passes the Request object to the function def index(req): s = """\ <html><head> <style type="text/css"> td {padding:0.2em 0.5em;border:1px solid black;} table {border-collapse:collapse;} </style> </head><body> <table cellspacing="0" cellpadding="0">%s</table> </body></html> """ attribs = '' # Loop over the Request object attributes for attrib in dir(req): attribs += '<tr><td>%s</td><td>%s</td></tr>' attribs %= (attrib, escape(unquote(str(req.__getattribute__(attrib))))) return s % (attribs)
Using the cgi.FormContentDict() may be simpler. Honestly, I haven’t used mod_cgi much so I’m not sure what all req has. I have built a lot of CGI scripts with Python though. I do:
import cgi
…
params = cgi.FormContentDict()
params is a Python dictionary. The dictionary key is the param name (for both GET and POST values) and the value is a list of all the values. In most cases, the list contain just one value.
By: Mike Heath on October 24, 2007
at 7:24 pm