[Tutor] please help with simple python CGI script

aivars aivars868 at gmail.com
Fri Oct 24 17:55:09 CEST 2008


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
>


More information about the Tutor mailing list