By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,621 Members | 1,074 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,621 IT Pros & Developers. It's quick & easy.

cgi python

P: n/a
I going to use the cgi-handler (mod_python):

http://www.modpython.org/live/mod_py.../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?

Oct 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Python_it wrote:
I going to use the cgi-handler (mod_python):

http://www.modpython.org/live/mod_py.../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

Oct 13 '05 #2

P: n/a
Python_it wrote:
I going to use the cgi-handler (mod_python):

http://www.modpython.org/live/mod_py.../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

Oct 13 '05 #3

P: n/a
Paul Boddie wrote:
Testing Web applications can be hard, but the traceback tells you
everything you need to know here.


especially if you make sure to use the cgitb module:

http://docs.python.org/lib/module-cgitb.html

while developing/debugging.

</F>

Oct 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.