[mod_python] FieldStorage and POST data

Jim Gallacher jpg at jgassociates.ca
Thu May 25 07:48:48 EDT 2006


Not enough information here to say for sure, but be aware that you can 
only create one instance of FieldStorage per request, as the body of the 
request gets "consumed". So if you do something like:

form1 = FieldStorage(req)
form2 = FieldStorage(req)

form1 will be populated with the contents of the POST but form2 will not.

When the FieldStorage instance is being created it will always parse the 
query string so you will always get those fields for a GET. For POST, 
the form data is encoded in the body of the request which FieldStorage 
treats as a file stream and creating form1 reads to the end of the 
stream. When you create form2, you are already at the end of the stream, 
so there is nothing to process.

Note that publisher automatically creates a FieldStorage instance as 
req.form. If you are using publisher, don't create your own instance. 
Use the one stuffed into req.form.

Jim

Gary Broadbent wrote:
> Sorry if this is a silly question, but:
> 
> I am having some issues trying to retrieve POST data from a form.
> 
> If I set the form method to GET, FieldStorage gets populated, but as soon as I 
> set the method to POST, I dont retrieve and data.
> 
> Please refer to the following output:
> 
> ***** GET method used **********
> FieldStorage  	
> = [Field('num', '0'),
>  Field('search', 'sear'),
>  Field('company', 'comp'),
>  Field('location', 'loca')]
> 
> the_request 
> = 'GET /sipnificant/results?num=0&search=sear&company=comp&location=loca 
> HTTP/1.1'
> 
> subprocess_env  	
> = {'DOCUMENT_ROOT': '/home/***/html',
>  'GATEWAY_INTERFACE': 'CGI/1.1',
>  'HTTP_ACCEPT': 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
>  'HTTP_ACCEPT_CHARSET': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
>  'HTTP_ACCEPT_ENCODING': 'gzip,deflate',
>  'HTTP_ACCEPT_LANGUAGE': 'en-us,en;q=0.5',
>  'HTTP_CONNECTION': 'keep-alive',
>  'HTTP_COOKIE': 'taskware_lang=en; style=A; 
> AWSUSER_ID=awsuser_id1143973127792r5484; colour=white',
>  'HTTP_HOST': 'plasmaperfect.net',
>  'HTTP_KEEP_ALIVE': '300',
>  'HTTP_REFERER': 'http://plasmaperfect.net/****/',
>  'HTTP_USER_AGENT': 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.0.1) 
> Gecko/20060222 Firefox/1.5.0.1',
>  'PATH': '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin:/usr/x86_64-pc-linux-gnu/gcc-bin/3.4.5',
>  'QUERY_STRING': 'num=0&search=sear&company=comp&location=loca',
>  'REMOTE_ADDR': '192.168.0.4',
>  'REMOTE_PORT': '53695',
>  'REQUEST_METHOD': 'GET',
>  'REQUEST_URI': '/****/results?num=0&search=sear&company=comp&location=loca',
>  'SCRIPT_FILENAME': '/home/***/sipnificant/results',
>  'SCRIPT_NAME': '/sipnificant/results',
>  'SERVER_ADDR': '192.168.0.2',
>  'SERVER_ADMIN': 'hostmaster at plasmaperfect.net',
>  'SERVER_NAME': 'plasmaperfect.net',
>  'SERVER_PORT': '80',
>  'SERVER_PROTOCOL': 'HTTP/1.1',
>  'SERVER_SIGNATURE': '',
> 
> unparsed_uri  	
> = '/sipnificant/results?num=0&search=sear&company=comp&location=loca'
> 
> uri 	
> = '/sipnificant/results'
> 
> filename 	
> = '/home/***/sipnificant/results'
> 
> canonical_filename 	
> = '/home/***/sipnificant/results'
> 
> path_info 	
> = ''
> args 	
> = 'num=0&search=sear&company=comp&location=loca'
> 
> finfo 	
> = None
> 
> parsed_uri 	
> = (None,
>  None,
>  None,
>  None,
>  None,
>  None,
>  '/sipnificant/results',
>  'num=0&search=sear&company=comp&location=loca',
>  None)
> 
> parsed_uri 	
> = (None,
>  None,
>  None,
>  None,
>  None,
>  None,
>  '/sipnificant/results',
>  'num=0&search=sear&company=comp&location=loca',
>  None)
> 
> ***** POST method used **********
> FieldStorage  	
> = []
> 
> the_request  	
> = 'POST /sipnificant/results HTTP/1.1'
> 
> subprocess_env  	
> = {'CONTENT_LENGTH': '44',
>  'CONTENT_TYPE': 'application/x-www-form-urlencoded',
>  'DOCUMENT_ROOT': '/home/***/html',
>  'GATEWAY_INTERFACE': 'CGI/1.1',
>  'HTTP_ACCEPT': 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
>  'HTTP_ACCEPT_CHARSET': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
>  'HTTP_ACCEPT_ENCODING': 'gzip,deflate',
>  'HTTP_ACCEPT_LANGUAGE': 'en-us,en;q=0.5',
>  'HTTP_CONNECTION': 'keep-alive',
>  'HTTP_COOKIE': 'taskware_lang=en; style=A; 
> AWSUSER_ID=awsuser_id1143973127792r5484; colour=white',
>  'HTTP_HOST': 'plasmaperfect.net',
>  'HTTP_KEEP_ALIVE': '300',
>  'HTTP_REFERER': 'http://plasmaperfect.net/sipnificant/',
>  'HTTP_USER_AGENT': 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.0.1) 
> Gecko/20060222 Firefox/1.5.0.1',
>  'PATH': '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin:/usr/x86_64-pc-linux-gnu/gcc-bin/3.4.5',
>  'QUERY_STRING': '',
>  'REMOTE_ADDR': '192.168.0.4',
>  'REMOTE_PORT': '37766',
>  'REQUEST_METHOD': 'POST',
>  'REQUEST_URI': '/sipnificant/results',
>  'SCRIPT_FILENAME': '/home/****/sipnificant/results',
>  'SCRIPT_NAME': '/sipnificant/results',
>  'SERVER_ADDR': '192.168.0.2',
>  'SERVER_ADMIN': 'hostmaster at plasmaperfect.net',
>  'SERVER_NAME': 'plasmaperfect.net',
>  'SERVER_PORT': '80',
>  'SERVER_PROTOCOL': 'HTTP/1.1',
>  'SERVER_SIGNATURE': '',
>  'SERVER_SOFTWARE': 'Apache'}
> 
> unparsed_uri  	
> = '/sipnificant/results'
> 
> uri 	
> = '/sipnificant/results'
> 
> args  	
> = None
> 
> finfo 	
> = None
> 
> parsed_uri 	
> = (None, None, None, None, None, None, '/sipnificant/results', None, None)
> 
> parsed_uri 	
> = (None, None, None, None, None, None, '/sipnificant/results', None, None)
> 
> 
> Thank you in advance,
> 
> Gary Stidston-Broadbent
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python
> 



More information about the Mod_python mailing list