Script error
Petter Larsen
hiof at online.no
Fri Feb 14 18:34:23 EST 2003
Hello!
I'am trying to get this guestbook script to work, but I get some errors I
don't understand.
Can someone please help me??
guestbook.py
----------------------------------------------------------------------------
-------------
import sys
sys.path.append('/usr/lib/python2.2')
import string, re, cgi, os
def ObsceneLanguage(usestring):
usestring=string.upper(usestring)
obscene="blast","sugar"
for useword in obscene:
if string.find(usestring, string.upper(useword))<>-1:
return (1)
return (0)
#usebody=open("guestbook.txt", "r") # Open main guestbook textfile
#usehead=open("guestbook-head.htm", "r") # Open guestbook HTML header
#usefoot=open("guestbook-foot.htm", "r") # Open guestbook HTML footer
if os.environ["QUERY_STRING"] == "post": # Performs the guestbook POST
useform=cgi.FieldStorage()
try:
realname= useform["REALNAME"].value
except:
realname=""
try:
email= useform["EMAIL"].value
except:
email=""
try:
location=useform["LOCATION"].value
except:
location=""
try:
comments=useform["COMMENTS"].value
except:
comments=""
comments=string.replace(comments, "\n", "<BR>");
print "Content-type: text/html"
print
print usehead.read()
if realname=="":
print "You have not entered a name/nickname"
print usefoot.read()
sys.exit(0)
elif comments=="":
print "You have not entered a comment"
print usefoot.read()
sys.exit(0)
if (ObsceneLanguage(realname)==1) or (ObsceneLanguage(email)==1) or
(ObsceneLanguage(email)==1) or (ObsceneLanguage(location)==1) or
(ObsceneLanguage(comments)==1):
print "You have entered something that would seem offensive. Please
press the back button to edit your text and resubmit."
print usefoot.read()
sys.exit(0)
n=now.now()
memBody=usebody.readlines()
memBody.insert(0,
realname+"||"+email+"||"+location+"||"+comments+"||"+str(n.day)+"/"+str(n.mo
nth)+"/"+str(n.year)+"\n")
usebody=open("guestbook.txt", "w")
usebody.writelines(memBody)
usebody.close
print "Many thanks for your guest book submission"
print "<A HREF=\"guestbook.py?view\">Click here</A> to view the guest
book"
else:
print "Content-type: text/html"
try:
print
print usehead.read()
bodyuse=usebody.readlines()
for useline in bodyuse:
usesplit=string.split(useline, '||')
print
print "<CENTER><TABLE width=\"100%\">"
print "<TR><TD align=right width=80><FONT
SIZE=\"-1\"><B>Name:</B></TD></FONT><TD><FONT SIZE=\"-1\"><A
HREF=\"mailto:"+usesplit[1]+"\">"+usesplit[0]+"</A></FONT></TD></TR>"
print "<TR><TD align=right width=80><FONT
SIZE=\"-1\"><B>Date:</B></TD></FONT><TD><FONT
SIZE=\"-1\">"+usesplit[4]+"</FONT></TD></TR>"
print "<TR><TD align=right width=80><FONT
SIZE=\"-1\"><B>Location:</B></FONT></TD><TD><FONT
SIZE=\"-1\">"+usesplit[2]+"</FONT></TD></TR>"
print "<TR><TD ALIGN=RIGHT width=80><FONT
SIZE=\"-1\"><B>Comments:</B></FONT></TD><TD BGCOLOR=\"#FFFFCC\"><FONT
SIZE=\"-1\">"+usesplit[3]+"</FONT></TD></TR>"
print "</TABLE></CENTER>"
print "<HR><P>"
print usefoot.read()
usehead.close();
usefoot.close();
except:
print "SORRY AN ERROR HAS OCCURRED"
Get the following error message:
----------------------------------------------------------------------------
-------------------
Traceback (most recent call last):
File "guestbook.py", line 28, in ?
if os.environ["QUERY_STRING"] == "post": # Performs the guestbook POST
File "/usr/lib/python2.2/UserDict.py", line 14, in __getitem__
def __getitem__(self, key): return self.data[key]
KeyError: QUERY_STRING
Best regards:
Petter Larsen
More information about the Python-list
mailing list