CGI on Medusa server
Sergio Boix Moriano
smoriano at sicon.net
Tue Dec 19 05:29:12 EST 2000
Hi all,
I've a medusa http server configured on WinNT, and i want to run a
cgi.
I'm triyng to do a POST command from an html form, but i get the
response HTTP 400. I suspect that i have to configure the http server
for execute cgi scripts, bu t i don't know how to do it. Any idea?
I send you the codes of the form and the cgi script
######### FORM.HTML ########
<FORM method="post" action="http://umbriel.pauta.es/cgi/simple.py">
<INPUT TYPE=HIDDEN NAME="key" VALUE="process">
Your name:<BR>
<INPUT TYPE=TEXT NAME="name" value="nombre" size=60>
<BR>
Email: (optional)<BR>
<INPUT TYPE=TEXT NAME="email" value="emilio" size=60>
<BR>
Favorite Color:<BR>
<INPUT TYPE=RADIO NAME="color" VALUE="blue" CHECKED>Blue
<INPUT TYPE=RADIO NAME="color" VALUE="yellow">No, Yellow...
<INPUT TYPE=RADIO NAME="color" VALUE="swallow">What do you
mean, an African or European swallow?
<P>
Comments:<BR>
<TEXTAREA NAME="comment" value="comentarios" ROWS=8 COLS=60>
</TEXTAREA>
<P>
<INPUT TYPE="SUBMIT" VALUE="prueba">
</FORM>
######### SIMPLE.PY #####
#! d:/python/python.exe
import re
import cgi
# specify the filename of the template file
TemplateFile = "c:/pysicon/ftp/html/template.html"
# specify the filename of the form to show the user
FormFile = "c:/pysicon/ftp/html/form.html"
# Display takes one parameter - a string to Display
def Display(Content):
TemplateHandle = open(TemplateFile, "r") # open in read only mode
# 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)
SubResult=[0,1]
print SubResult
if SubResult[1] == 0:
raise BadTemplateException
print "Content-Type: text/html\n\n"
print SubResult[0]
### what follows are our two main 'action' functions, one to show the
### form, and another to process it
# this is a really simple function
def DisplayForm():
FormHandle = open(FormFile, "r")
FormInput = FormHandle.read()
FormHandle.close()
Display(FormInput)
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:
color = form["color"].value
except:
color = 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:\"" + email + "">"" + name +
"</A>.<P>"
else:
Output = Output + name + ".<P>"
if color == "swallow":
Output = Output + "You must be a Monty Python fan.<P>"
elif color != None:
Output = Output + "Your favorite color was " + color + "<P>"
else:
Output = Output + "You cheated! You didn't specify a color!<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()
Thanks in advance for all your responses
More information about the Python-list
mailing list