cgi python

P

Python_it

I going to use the cgi-handler (mod_python):

http://www.modpython.org/live/mod_python-3.2.2b/doc-html/hand-cgi.html


If I test a simply py script it works

code:
===========
print "Content-type: text/html\n"
print """
<html>
<body>
<h1>TEST</h1>
</body>
</html>
"""
============

But if I test a py script with cgi comments (import cgi):

Part of the code
============
import cgi

#get HTTP query parameters
query = cgi.FieldStorage()

#Get the selected year and month
selectedYear = int(query["year"].value)
selectedMonth = int(query["x"].value) + 1

#Get the monthly revenue
monthlyRevenue = float(query["value"].value)
============

I get the following error:

errormessage:
==========================
Mod_python error: "PythonHandler mod_python.cgihandler"

Traceback (most recent call last):

File "C:\Program
Files\Python24\Lib\site-packages\mod_python\apache.py", line 299, in
HandlerDispatch
result = object(req)

File "C:\Program
Files\Python24\Lib\site-packages\mod_python\cgihandler.py", line 96, in
handler
imp.load_module(module_name, fd, path, desc)

File "C:/Program Files/Apache
Group/Apache2/htdocs/clue/modules/templates\clickpie.py", line 9, in ?
selectedYear = int(query["year"].value)

File "C:\Program Files\Python24\Lib\cgi.py", line 559, in __getitem__
raise KeyError, key

KeyError: 'year'


Who can help me?
 
C

Christian Hausknecht

Python_it said:
I going to use the cgi-handler (mod_python):

http://www.modpython.org/live/mod_python-3.2.2b/doc-html/hand-cgi.html


If I test a simply py script it works

code:
===========
print "Content-type: text/html\n"
print """
<html>
<body>
<h1>TEST</h1>
</body>
</html>
"""
============

But if I test a py script with cgi comments (import cgi):

Part of the code
============
import cgi

#get HTTP query parameters
query = cgi.FieldStorage()

#Get the selected year and month
selectedYear = int(query["year"].value)
selectedMonth = int(query["x"].value) + 1

#Get the monthly revenue
monthlyRevenue = float(query["value"].value)
============

I get the following error:

errormessage:
==========================
Mod_python error: "PythonHandler mod_python.cgihandler"

Traceback (most recent call last):

File "C:\Program
Files\Python24\Lib\site-packages\mod_python\apache.py", line 299, in
HandlerDispatch
result = object(req)

File "C:\Program
Files\Python24\Lib\site-packages\mod_python\cgihandler.py", line 96, in
handler
imp.load_module(module_name, fd, path, desc)

File "C:/Program Files/Apache
Group/Apache2/htdocs/clue/modules/templates\clickpie.py", line 9, in ?
selectedYear = int(query["year"].value)

File "C:\Program Files\Python24\Lib\cgi.py", line 559, in __getitem__
raise KeyError, key

KeyError: 'year'


Who can help me?
There is no Parameter 'year' in the Format-String sent by the Webserver!
You can test it like this:
if(query.has_key("yaer")):
xyz = query["year"].value

If you want to get a Parameter called 'year', you must call the cgi-Script
like this:
http://host/path-to-cgi-dir/script.py?year=2005

Another method is, to use a form in the calling html-Page and put a hidden
value into it.

Ciao,
Christian
 
P

Paul Boddie

Python_it said:
I going to use the cgi-handler (mod_python):

http://www.modpython.org/live/mod_python-3.2.2b/doc-html/hand-cgi.html

If I test a simply py script it works

You don't say how you test it, but I imagine that you just point your
browser to the location where the program is published, without
specifying any query parameters - see below for the significance of
this.

[...]
But if I test a py script with cgi comments (import cgi):

Part of the code
============
import cgi

#get HTTP query parameters
query = cgi.FieldStorage()

#Get the selected year and month
selectedYear = int(query["year"].value)
[...]

KeyError: 'year'

The problem is that if you point your browser to this new program and
don't specify query parameters (eg. ?year=2005&x=10&value=5000 on the
end of the URL) then attempting to get the "year" parameter from the
query object will fail because the parameter doesn't exist - you didn't
specify it.

What you should do is to test for its presence first, just like you
would with a normal Python dictionary, and I believe that the query
object supports the has_key method:

if query.has_key("year"):
selectedYear = int(query["year"].value)

A more comprehensive alternative, given that the "year" parameter may
not be a number, is to catch any exceptions raised when you try and get
the parameter's value:

try:
selectedYear = int(query["year"].value)
except KeyError:
Do something here about the missing parameter.
except ValueError:
Do something here about a non-integer parameter.

Testing Web applications can be hard, but the traceback tells you
everything you need to know here.

Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top