[Tutor] Help with 500 error on cgi script
Wesley J. Chun
wesc@deirdre.org
Sun, 18 Mar 2001 18:08:45 -0800
Úÿ¿¢ûàÿàÿ
> From: Sheila King <sheila@spamcop.net>
> Subject: [Tutor] Help with 500 error on cgi script
> Date: Sun, 18 Mar 2001 01:33:57 -0800
>
> ... [snip] ...
> if form.has_key("subscriberEmail") and form.has_key("command"):
> form_ok = 1
> if not form_ok:
> output = '''Content-type: text/html\n\n
> <html>
> ... [snip] ...
> </body>\n</html>
> '''
> print output
> else:
> for k in form.keys():
> print k, " : ", form[k]
> print "All done!"
sheila,
as others have pointed out in earlier posts, there were 2 basic
problems with the script, that's it! everything else was fine.
you probably already know this, but playing with CGI is a 3-way game.
the (1) user or web client makes a call to the (2) web server for some data.
becuse the web server is too "dumb" to be able to comprehend form data,
it must ask (3) a 3rd party application (via the Common Gateway Interface)
to do the real work and just give it back some HTML to return to the client.
a web server only knows about the (MIME) header it needs to send back and
raw HTML (whether from a static .html file or from a CGI application).
(1) Error 500s or ISEs (Internal Server Errors) occur when the web
server gets either a bad header or invalid HTML. it pukes and
sends a 500 back to the web client. so whenever you get these,
it's a sign that somehow your CGI app messed up somehow.
in your code above, altho you handle the error situation just
fine, your "it's ok" code does not send back a header, i.e.
"Content-Type:"... nor does it send back valid HTML. instead,
it does:
'print k, " : ", form[k]'
... which is not HTML at all, out comes a 500; and once you fix
*that* bug, you'll get another 500 with:
print "All done!"
... which is also not HTML. Surround your output with the ap-
propriate HTML doo-dads and you'll be okay.
(2) daniel also pointed out that you should not mix spaces and TABs
and that is also true. in fact, just avoiding use of TABs works
out best. TABs are represented with a different number of spaces
on each system so source files are not easily read on other
systems. sticking with pure spaces will allow you to edit the
file without inconsistency across different platforms.
one way you can avoid problems such as (1) is to make your output
interface consistent. here's one way:
# whether okay or error, same header is returned
output = 'Content-type: text/html\n\n'
# okay output
if form.has_key("subscriberEmail") and form.has_key("command"):
# all okay output has this header
output += '<HTML><BODY><H1>YEAH!!</H1>\n'
# build form item list
for k in form.keys():
output += "%s : %s<BR>\n" % (k, form[k])
# end okay output
output += 'All done!</BODY></HTML>'
# error output
else:
# use error string output
output += '<HTML><BODY><H1>YOU GOOFED!</BODY></HTML>'
# whether okay or error, send the output (same variable!)
print output
---
hope this helps!
-wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Silicon Valley-SF Bay Area Python users group: http://www.baypiggies.org
"Core Python Programming", Prentice Hall PTR, December 2000
http://www.phptr.com/ptrbooks/ptr_0130260363.html
wesley.j.chun :: wesc@baypiggies.org
cyberweb.consulting :: silicon.valley, ca
http://www.roadkill.com/~wesc/cyberweb/