[Tutor] Please help me with my first CGI program???

Lloyd Kvam lkvam@venix.com
Mon, 19 Nov 2001 14:50:01 -0500


The Perl CGI module will prompt you for inputs when you run a Perl CGI 
script standalone.  Python isn't as obvious.

form = cgi.FieldStorage()            # parse form data

This does not give you a valid form dictionary when run standalone.  You 
must build the dictionary by hand.  Something along the lines of:

debugging = 1
if debugging:
	form = { 'easelearning' : '1',
		'easeuse' : '2',
...
	}

You could put the debugging code just under the call to FieldStorage. 
Remember to turn off debugging (debugging = 0) before copying this to 
your web server.

Ron Stephens wrote:

> Any help would be appreciated. I am getting a KEYERROR when I run my CGI
> program as a standalone python program. Maybe its just because the
> standalone python program doesn't have the input data from the FORM I
> use in my HTML input page to my CGI. Howver, I get no error messages
> from actaully running my CGI on the web (just a general serer error),
> even though I set sys.stderr = sys.stdout
> 
> I know I'm a clueless newbie, but even any general help will be greatly
> appreciated. I think I am almost there, but I can't figure out a way to
> effectively debug my CGI program.
> 
> IDLE gives me the following error message when I run the program in IDLE
> as a stand-alone program::
> 
> Content-type: text/html
> Traceback (most recent call last):
>   File "C:/Python21/aaacgi.py", line 26, in ?
>     rankings = {"ease of learning" : form['easelearning'].value,
>   File "C:\PYTHON21\lib\cgi.py", line 547, in __getitem__
>     raise KeyError, key
> KeyError: easelearning
> 
> The input HTML page is:::
> 
> <html><body>
> <title>CGI 101</title>
> <H1>A first user interaction: forms</H1>
> <hr>
> <form method=POST action="http://www.awaretek.com/test3.cgi">
> 
> Input an integer from 1 to 100, as a weight or relative importance
> factor for each of the citeria below. 1 means not very importnat to you,
> 100 menas extremely important to you
> 
> 
> <P><B>ease of learning</B>
>     <input type=text name=easelearning>
> 
>  <P><B>ease of use</B>
>     <input type=text name=easeuse>
> 
>  <P><B>speed of program execution</B>
>     <input type=text name=speed>
> 
>  <P><B>quality of available tools</B>
>     <input type=text name=tools>
> 
>  <P><B>popularity</B>
>     <input type=text name=pop>
> 
>  <P><B>power and expressiveness</B>
>     <input type=text name=power>
> 
>  <P><B>cross-platform</B>
>     <input type=text name=cross>
> 
>  <P><B>cost</B>
>     <input type=text name=cost>
>     <input type=submit>
> </form>
> </BODY></HTML>
> 
> 
> The CGIU program follows. The data for FORM is fed into the cgi program
> by the HTML FORM above:::
> 
> #!/usr/bin/python
> #######################################################
> # runs on the server, reads form input, prints html;
> # url=http://server-name/root-dir/Basics/test3.cgi
> #######################################################
> 
> import cgi
> import string
> import sys
> sys.stderr = sys.stdout
> form = cgi.FieldStorage()            # parse form data
> print "Content-type: text/html"      # plus blank line
> 
> html = """
> <TITLE>test3.cgi</TITLE>
> <H1>Greetings</H1>
> <HR>
> <P>%s</P>
> <HR>"""
> 
> 
> 
> options = ["Python", "Perl", "Ruby", "Tcl", "JavaScript", "Visual
> Basic", "Java", "C++", "C", "Lisp", "Delphi"]
> criteria = ["ease of learning", "ease of use", "speed of program
> execution", "quality of available tools", "popularity", "power &
> expressiveness", "cross platform?", "cost"]
> 
> rankings = {"ease of learning":form['easelearning'].value,
> 
> 
> "ease of learning" : form['easelearning'].value,
> "ease of use" = form['easeuse'].value,
> "speed" = form['speed'].value,
> "tools" = form['tools'].value,
> "pop" = form['pop'].value,
> "power" = form['power'].value,
> "cross"] = form['cross'].value,
> "cost"] = form['cost'].value}
> 
> score = {("Python", "ease of learning"):100, ("Python", "ease of
> use"):100, ("Python", "speed of program execution"):10, ("Python",
> "quality of available tools"):70, ("Python", "popularity"):50,
> ("Python", "power & expressiveness"):100, ("Python", "cross
> platform?"):100, ("Python", "cost"):100,
> ("Perl", "ease of learning"):50, ("Perl", "ease of use"):90, ("Perl",
> "speed of program execution"):30, ("Perl", "quality of available
> tools"):50, ("Perl", "popularity"):75, ("Perl", "power &
> expressiveness"):100, ("Perl", "cross platform?"):100, ("Perl",
> "cost"):100,
> ("Ruby", "ease of learning"):50, ("Ruby", "ease of use"):100, ("Ruby",
> "speed of program execution"):20, ("Ruby", "quality of available
> tools"):20, ("Ruby", "popularity"):10, ("Ruby", "power &
> expressiveness"):100, ("Ruby", "cross platform?"):80, ("Ruby",
> "cost"):100,
> ("Tcl", "ease of learning"):100, ("Tcl", "ease of use"):100, ("Tcl",
> "speed of program execution"):10, ("Tcl", "quality of available
> tools"):50, ("Tcl", "popularity"):40, ("Tcl", "power &
> expressiveness"):10, ("Tcl", "cross platform?"):100, ("Tcl",
> "cost"):100,
> ("JavaScript", "ease of learning"):70, ("JavaScript", "ease of use"):75,
> ("JavaScript", "speed of program execution"):10, ("JavaScript", "quality
> of available tools"):50, ("JavaScript", "popularity"):100,
> ("JavaScript", "power & expressiveness"):40, ("JavaScript", "cross
> platform?"):50, ("JavaScript", "cost"):100,
> ("Visual Basic", "ease of learning"):50, ("Visual Basic", "ease of
> use"):100, ("Visual Basic", "speed of program execution"):20, ("Visual
> Basic", "quality of available tools"):100, ("Visual Basic",
> "popularity"):100, ("Visual Basic", "power & expressiveness"):50,
> ("Visual Basic", "cross platform?"):1, ("Visual Basic", "cost"):1,
> ("Java", "ease of learning"):15, ("Java", "ease of use"):50, ("Java",
> "speed of program execution"):50, ("Java", "quality of available
> tools"):100, ("Java", "popularity"):100, ("Java", "power &
> expressiveness"):100, ("Java", "cross platform?"):100, ("Java",
> "cost"):100,
> ("C++", "ease of learning"):10, ("C++", "ease of use"):25, ("C++",
> "speed of program execution"):90, ("C++", "quality of available
> tools"):100, ("C++", "popularity"):100, ("C++", "power &
> expressiveness"):100, ("C++", "cross platform?"):100, ("C++",
> "cost"):100,
> ("C", "ease of learning"):15, ("C", "ease of use"):10, ("C", "speed of
> program execution"):100, ("C", "quality of available tools"):100, ("C",
> "popularity"):100, ("C", "power & expressiveness"):100, ("C", "cross
> platform?"):110, ("C", "cost"):100,
> ("Lisp", "ease of learning"):20, ("Lisp", "ease of use"):30, ("Lisp",
> "speed of program execution"):70, ("Lisp", "quality of available
> tools"):50, ("Lisp", "popularity"):25, ("Lisp", "power &
> expressiveness"):110, ("Lisp", "cross platform?"):80, ("Lisp",
> "cost"):90,
> ("Delphi", "ease of learning"):50, ("Delphi", "ease of use"):110,
> ("Delphi", "speed of program execution"):85, ("Delphi", "quality of
> available tools"):100, ("Delphi", "popularity"):30, ("Delphi", "power &
> expressiveness"):100, ("Delphi", "cross platform?"):80, ("Delphi",
> "cost"):10}
> 
> result = {}
> for o in options:
>         value = 0
>         for c in criteria:
> 
>                 value = value + rankings[c] * score[o, c]
>         result[o] = value
> 
> results = result.items()        # A list of tuples (key, value)
> results.sort(lambda x, y: -cmp(x[1], y[1]))
>                                 # Sort the list using the reverse of the
> 
>                                 # "value" of the entry, so that higher
>                                 # values come first
> 
> 
> print html % ("Hello, %s." % form['easeuse'].value)
> 
> print html % form['easeuse'].value
> 
> 
> 
> 
> 
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 


-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice: 
603-443-6155
fax: 
801-459-9582