[mod_python] help needed with forms' parameters handling

Petar Karafezov petar at metamarkets.com
Fri Mar 9 10:20:43 EST 2001


Hello all -

I am running Apache/1.3.14 (Unix) mod_python/2.7.2 Python/2.0. At this point
I am not even sure that this is a mod_python issue, as I have seen this
same behavior before I ported my cgi to mod_python. I am not sure what to do
at this point..

Here's the issue:

When you do a POST to a python script you obtain the parameters using the
util.FieldStorage, when using cgi it's cgi.FieldStorage. The returned result
is a dictionary { key, value }. Everything is OK if you just what to work
with this dictionary. However, if you try to pass this dictionary object to
urllib.urlencode, we raise an exception:

Mod_python error: "PythonHandler parse2"

Traceback (most recent call last):

  File "/usr/local/lib/python2.0/site-packages/mod_python/apache.py", line
189, in Dispatch
    result = object(self.req)

  File "/usr/local/apache/htdocs/test/parse2.py", line 147, in handler
    params=urllib.urlencode(form)	

  File "/usr/local/lib/python2.0/urllib.py", line 1086, in urlencode
    for k, v in dict.items():

AttributeError: 'FieldStorage' instance has no attribute 'items'


Now, the funny part is that if I get this object 'form' - which was defined
as form=util.FieldStorage(req) and just get the keys and values and form a
new dictionary, I can pass that new one to urllib.urlencode with no
problems.

...
    newparams={}                       
    for key in form.keys():
          newparams[key]=form[key]
    params=urllib.urlencode(newparams) 
...

Again, I run into the same issue with both util.FieldStorage when working
with mod_python and with cgi.FieldStorage when using my cgi version.

I am quite happy with this workaround until I bumped into a different issue,
which I am not exactly sure if it's related or not. When there're multiple
checkboxes on a form:

<input type="checkbox"  name="frequencies" value="DEM" priority="6" checked>
<input type="checkbox"  name="frequencies" value="HLF" priority="6">
<input type="checkbox"  name="frequencies" value="DAY" priority="6">
<input type="checkbox"  name="frequencies" value="WEK" priority="6">

and we have selected more than 1, then the form parameter value gets passed
to the server as an array. For example, if I'd select DEM and DAY from the
checkboxed above, my params would be:

'frequences' : ['DEM','DAY']

This is all great, but when I pass the parameters to the server - my python
script is playing the role of a proxy, then I am passing as value for
'frequences' the STRING "['DEM','DAY']" and not the ARRAY ['DEM','DAY']. 

So, somewhere in what I do/use the array gets converted to a string. The way
I see it is:

1) It could be the python's FieldStorage when I originally get the keys and
values
2) It could be that funky dictionary rewriting that I am doing before I pass
the form parameters to urllib.urlencode.
3) I could be something in what urlencode does.


I am not sure where to look at this point. 

Any ideas, anyone ?? Any help would be greatly appreciated!

Thanks for your help!

regards,

Petar Karafezov

MetaMarkets.com
415-575-3015
-------------------------------------------
Investing Out Loud at
http://www.metamarkets.com
-------------------------------------------





More information about the Mod_python mailing list