[mod_python] using FieldStorage without publishing handler

Nic James Ferrier nferrier at tapsellferrier.co.uk
Fri Apr 20 11:56:08 EDT 2007


"Ruben K." <midreth at gmail.com> writes:

> Hello,
>
> I was wondering if it is possible to use FieldStorage for POST data and FILE
> data
> without having to use the publishing handler. Is it possible? How would I go
> about this?
>
> The only kind of data I have been able to get with it is GET data

I think you must be doing something wrong because it just works:

  def handler(http):
      params = util.FieldStorage(http)
      for field in params.list:
          print >>http, 
               """<span class="%s">%s</span>""" %
                       (field.name, field.value)

will spit out some values with GET or POST.

It seems to prefer either one of the URI args or the
multipart/form-data args so if you're doing this:

  POST /someuri?name=value
  content-type: application/x-www-form-urlencoded
  other=value

then you need to have something combines the two.

I use this:

  def http_params(http):
      mod_python_form = util.FieldStorage(http)
      fields = mod_python_form.list
      result_dict = {}
      try:
          result_dict = util.parse_qs(http.args, True)
      except TypeError:
          # The above can produce an error coz http.args is None
          pass

      for field in fields:
          result_dict[field.name] = field.value
      return result_dict

But this also normalizes the fielstorage to a normal dict and I
suspect there is more elegant python that will do the job much better.

-- 
Nic Ferrier
http://www.tapsellferrier.co.uk   


More information about the Mod_python mailing list