[Tutor] please help with simple python CGI script

aivars aivars868 at gmail.com
Sun Oct 26 07:53:52 CET 2008


Thanks John and Alan

I get the following error when I run the script from IDLE:

Traceback (most recent call last):
  File "C:\Cgi-bin\friends1.py", line 15, in <module>
    who = form['person'].value
  File "C:\Python25\lib\cgi.py", line 567, in __getitem__
    raise KeyError, key
KeyError: 'person'


Aivars
Windows XP, python 2.5

2008/10/26 John Pomni <jpomni at googlemail.com>:
> what error do you get? It works without any changes on my Linux machine
> with python 2.5
>
> John
>
> On Sat, 2008-10-25 at 21:33 +0300, aivars wrote:
>> It does not work neither as script or from command line. I will try to
>> find the guy Wesley Chun and ask him
>>
>> 2008/10/25 John Pomni <jpomni at googlemail.com>:
>> > Hi,
>> >
>> > The script does not work from command line but I guess you do not have
>> > any problems running it as CGI?
>> >
>> > I like cgitb modules for debugging purposes very much.
>> >
>> > http://www.python.org/doc/2.5.2/lib/node566.html
>> >
>> > Jan
>> >
>> > On Fri, 2008-10-24 at 18:55 +0300, aivars wrote:
>> >> Thanks very much, Kent,
>> >>
>> >> So it seems that directory /cgi-bin should be a subdirectory to that
>> >> directory from which the web server was started/is running. That
>> >> worked and Deitel's script - getting time displayed finally worked.
>> >>
>> >> still banging mu head with Wesley Chun's simple example -
>> >>
>> >> #!C:\python25\python.exe
>> >>
>> >> import cgi
>> >>
>> >> reshtml = '''Content-Type: text/html\n
>> >> <HTML><HEAD><TITLE>
>> >> Friends CGI Demo (dynamic screen)
>> >> </TITLE></HEAD>
>> >> <BODY><H3>Friends list for: <I>%s</I></H3>
>> >> Your name is: <B>%s</B><P>
>> >> You have <B>%s</B> friends.
>> >> </BODY></HTML>'''
>> >>
>> >> form = cgi.FieldStorage()
>> >> who = form['person'].value
>> >> howmany = form['howmany'].value
>> >> print reshtml % (who, who, howmany)
>> >>
>> >> It gives me the following error:
>> >> Traceback (most recent call last):
>> >>   File "C:\Cgi-bin\friends1.py", line 15, in <module>
>> >>     who = form['person'].value
>> >>   File "C:\Python25\lib\cgi.py", line 567, in __getitem__
>> >>     raise KeyError, key
>> >> KeyError: 'person'
>> >>
>> >> I understand python is saying that there is no such a key in a directory.
>> >>
>> >> The HTML form looks like this. it is displayed correctly both in FF and IE
>> >>
>> >>
>> >> <HTML><HEAD><TITLE>
>> >> Friends CGI Demo (static screen)
>> >>    </TITLE></HEAD>
>> >>   <BODY><H3>Friends list for: <I>NEW USER</I></H3>
>> >>    <FORM ACTION="/cgi-bin/friends1.py">
>> >>   <B>Enter your Name:</B>
>> >>   <INPUT TYPE="text" NAME="person" VALUE="NEW USER" SIZE=15>
>> >>    <P><B>How many friends do you have?</B>
>> >>    <INPUT TYPE="radio" NAME="howmany" VALUE="0" CHECKED> 0
>> >>   <INPUT TYPE=radio NAME="howmany" VALUE="10"> 10
>> >>   <INPUT TYPE="radio" NAME="howmany" VALUE="25"> 25
>> >>   <INPUT TYPE="radio" NAME="howmany" VALUE="50"> 50
>> >>   <INPUT TYPE="radio" NAME="howmany" VALUE="100"> 100
>> >>   <P><INPUT TYPE="submit"></FORM></BODY></HTML>
>> >>
>> >> Thanks again,
>> >>
>> >> Aivars
>> >>
>> >>
>> >>
>> >>
>> >>
>> >>
>> >> 2008/10/24 Kent Johnson <kent37 at tds.net>:
>> >> > On Fri, Oct 24, 2008 at 10:25 AM, aivars <aivars868 at gmail.com> wrote:
>> >> >> Hello,
>> >> >>
>> >> >> I am learning python.
>> >> >>
>> >> >> I start the python CGI server like this:
>> >> >>
>> >> >> python -m CGIHTTPServer (this syntax I saw in Wesley Chun's Core
>> >> >> Python Programming chapter 20.5)
>> >> >>
>> >> >> The server starts in command prompt on windows XP by answering:
>> >> >> Serving HTTP on 0.0.0.0 port 8000...
>> >> >>
>> >> >> Next I want to run this simple CGI script (from Deitel Python How to
>> >> >> Program chapter 6). it is supposed to print out current date and time
>> >> >> in a browser
>> >> >
>> >> > The CGI script should me in a /cgi-bin subdirectory of the dir where
>> >> > you run the script. The URL to run the CGI will then be something like
>> >> > http:://localhost:8000/cgi-bin/myscript.py
>> >> >
>> >> > Kent
>> >> >
>> >> _______________________________________________
>> >> Tutor maillist  -  Tutor at python.org
>> >> http://mail.python.org/mailman/listinfo/tutor
>> >
>> >
>
>


More information about the Tutor mailing list