[mod_python] XMLHttpRequest + CGI + mod_python

Nando Drabik nandogringo at gmail.com
Thu Aug 10 16:03:39 EDT 2006


Hi all,

I would like some help fixing the problem I am stuck in. To make this as
short and simple as possible, it has mostly to do with the
cgi.FieldStorage class. I have:

1) mod_python installed correctly on apache2
2) File test.py (resides under cgi-bin):
3) HTML file that sends out XMLHttpRequest from js

test.py ===============================
#!/usr/bin/env python

import sys, re, cgi
import cgitb; cgitb.enable(display=0,logdir=".")
sys.path.insert(0,'/opt/atlas/lib/python')
from jsonutils import jsonrpc

htmlTemplate = """Content-Type: text/html

<html>
<head><title>%(title)s</title></head>
<body>
%(body)s
</body></html>
"""
fields = cgi.FieldStorage()
req = fields.getvalue("myVar") ##  THIS IS WHERE THE WHOLE THING CRAHSES

print htmlTemplate % { 'title': "Result", 'body': req}
=====================================

When I access /server/cgi-bin/test.py?myVar='content', by directly
typing the URL on firefox, I do receive back "content", no problem.

However, from an html file, if I send and XMLHttpRequest in javascript
like this:
HTML/JS snippet ===========================
var req = new XMLHttpRequest();
rep.open("post", "http://server/cgi-bin/test.py")
req.send("myVar=10"):
=====================================

The script crashes when the request is sent. After debugging, I've
realized that the problem is in the FieldStorage class. Here's the
output from the error log:

error log ===============================
 problem occurred in a Python script. Here is the sequence of function
calls leading up to the error, in the order they occurred.
 /srv/www/cgi-bin/atlas/test.py

   28         fields = cgi.FieldStorage()
   29         req = fields.getvalue("myVar")

req undefined, fields = FieldStorage(None, None, 'myVar=10'),
fields.getvalue = <bound method FieldStorage.getvalue of
FieldStorage(None, None, 'myVar=10')>
 /usr/lib/python2.4/cgi.py in getvalue(self=FieldStorage(None, None,
'myVar=10'), key='myVar', default=None)
  565     def getvalue(self, key, default=None):
  566         """Dictionary style get() method, including 'value' lookup."""
  567         if key in self:
  568             value = self[key]
  569             if type(value) is type([]):
key = 'myVar', self = FieldStorage(None, None, 'myVar=10')
 /usr/lib/python2.4/cgi.py in __contains__(self=FieldStorage(None, None,
'myVar=10'), key='myVar')
  616         """Dictionary style __contains__ method."""
  617         if self.list is None:
  618             raise TypeError, "not indexable"
  619         for item in self.list:
  620             if item.name == key: return True
builtin TypeError = <class exceptions.TypeError>

TypeError: not indexable
      args = ('not indexable',)
====================================================

I am new to python, so I'm a bit confused. If I print the variable
'fields' in test.py, it displays the list (FieldStorage(None, None,
'myVar=10')) just fine, but the methods getvalue, getfirst, etc raise
TYPE exceptions(not indexable). Interestingly, the has_key method works.

So, I have been unable to retrieve the post data from the
XMLHttpRequest. Can anyone help me figure out how to access the post
variables from mod_python residing under a cgi-bin directory?

Thank you very much in advance!
Fernando

	
Fernando Drabik <fernando.drabik at noaa.gov>
OceanEye Web Application Developer
Coral Reef Ecosystem Division
NOAA Fisheries


More information about the Mod_python mailing list