[Tutor] Server Error
Ms Soo Chong
s4046441 at student.uq.edu.au
Sun Aug 22 08:02:11 CEST 2004
Hi,
Thanks Danny for your prompt reply.
Actually, the server error shown in my previous mail
includes import cgitb; cgitb.enable(), thats why I
don't understand. Sorry that I didn't make myself
clear. But still thanks to you.
The following is my basic4.py, please help me to
take a look. I sort of typed in word by word as
shown in devshed. So please pardon me if I typed in
some stuffs that is not needed. ;D
###################################################################
#!/usr/bin/python
#Created on: 20/08/04
import sys
sys.stderr = sys.stdin
#Import the regular expression module.
import re
#Import the CGI module.
import cgi
import cgitb; cgitb.enable()
#Specify the filename of the template file.
TemplateFile = "/html/sf/template.html"
#Specify the filename of the form to show the user.
FormFile = "/html/sf/form.html"
#Define a new function called Display.
#it takes one parameter - a string to Display.
def Display(Content):
TemplateHandle = open(TemplateFile, "r") #open
in read mode only.
#read the entire file as a string
TemplateInput = TemplateHandle.read()
TemplateHandle.close() #close
the file.
#This defines an exception string in case
#our template file is messed up.
BadTemplateException = "There was a problem with
the HTML template."
SubResult = re.subn("<!--***INSERT CONTENT
HERE***--",
Content, TemplateInput)
if SubResult[1] == 0:
raise BadTemplateException
#Required header that tells the browser how to
render the HTML.
print"Content-Type: text/html\n\n"
print SubResult[0]
###What follows are the two main 'action' functions.
###One to show the form and another to process it.
#Define another function for form.
def DisplayForm():
FormHandle = open(FormFile, "r")
FormInput = FormHandle.read()
FormHandle.close()
Display(FormInput)
#Define a function to process the form.
def ProcessForm(form):
#extract the information from the form in easily
digestible format.
try:
name = form["name"].value
except:
#name is required. so output an error
#if not given and exit script
Display("You need to at least supply a name.
Please go back.")
raise SystemExit
try:
email = form["email"].value
except:
email = None
try:
colour = form["colour"].value
except:
colour = None
try:
comment = form["comment"].value
except:
comment = None
Output = "" #our output buffer, empty at first.
Output = Output + "Hello,"
#if email != None:
#Output = Output + "<A
HREF="mailto:s4046441 at student.uq.edu.au" + email
#+ "">" + name + "</A>.<P>"
#else:
#Output = Output + name + ".<P>"
if colour == "swallow":
Output = Output + "You must be a Monty
Python fan.<P>"
elif colour != None:
Output = Output + "Your favourite colour was
"+colour+"<P>"
else:
Output = Output + "You cheated! You didn't
specify a colour!<P>"
if comment != None:
Output = Output + "In addition, you
said:<BR>"+comment+"<P>"
Display(Output)
###Begin actual script.
###Evaluate CGI request
form = cgi.FieldStorage()
###"key" is a hidden form element with an
###action command such as "process".
try:
key = form["key"].value
except:
key = None
if key == "process":
ProcessForm(form)
else:
DisplayForm()
#########################################################
Sorry that the script is messed up. Actually, that
is not its original look, don't know why it messed
up in this mail. (Sorry again)
Another error was detected if I include the following:
#############################################################
if email != None:
Output = Output + "<A
HREF="mailto:s4046441 at student.uq.edu.au" + email
+ "">" + name + "</A>.<P>"
else:
Output = Output + name + ".<P>"
#############################################################
The error msg is: (I can't copy and paste so I typed
it manually)
File "<stdin>", line 90
Output = Output + "<A
HREF="mailto:s4046441 at student.uq.edu.au" + email
^
SynataxError = invalid syntax
#############################################################
See the attached file if you can't read the above
script. Sorry.
Thank you in advance for whoever can tell me whats
wrong with this script.
Shufen
-------------- next part --------------
A non-text attachment was scrubbed...
Name: basic4.py
Type: application/octet-stream
Size: 3072 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tutor/attachments/20040822/9fb98537/basic4.obj
More information about the Tutor
mailing list