[Tutor] CGI File Woes

wormwood_3 wormwood_3 at yahoo.com
Sun Sep 30 22:26:59 CEST 2007


Those examples were a lot of help Martin. Turned out the only issue was that I did not have this line right:

print "Content-type: text/html\n\n"

With that form, it loaded just fine. It had been running fine from the terminal, but without this line being right, Apache did not know what to do with it.

I had spoken with my web-hosting provider, but since I had a shared account I was unable to view the server logs. And the person helping me knew nothing about Python (he kept slipping and calling it PHP actually, to my dismay and chagrin:-) ).

Thanks for all the help, Alan and Martin.

-Sam

_________________
----- Original Message ----
From: Martin Walsh <mwalsh at groktech.org>
To: tutor at python.org
Sent: Sunday, September 30, 2007 1:07:02 PM
Subject: Re: [Tutor] CGI File Woes

No doubt cgitb is a great tool for debugging cgi, but IIUC there are at
least two instances when you will not get the pretty printed tracebacks
in the browser when using cgitb. One is after, what I would call, a
'compile time' exception such as SyntaxError, in your python code. The
other is when the python code runs without exception, but you have not
separated the header and the document content with a newline. At least,
I have found these to be true when using apache-cgi.

Consider the following examples:

#!/usr/bin/env python
# raises a SyntaxError

import cgi
import cgitb; cgitb.enable()

print "Content-type: text/html\n\n"
# NOTE: purposeful misspelling of the print statement
prin "<html><body><p>Hello, world!</p></body></html>"

# the code above will produce a server 500, with apache
# complaining about "premature end of script headers"

...

#!/usr/bin/env python
# good python, bad data

import cgi
import cgitb; cgitb.enable()

print "Content-type: text/html"
print "<html><body><p>Hello, world!</p></body></html>"

# this is syntactically correct python, and will
# run from the command line, but the html header
# and html content have no separation, so apache
# will consider all of it to be header info, resulting
# in another server 500, "malformed header"

As others have advised, under these circumstances you can review the
apache error log (if your webhost allows it). Or roll-your-own logging
equivalent, and run your cgi from a terminal to catch SyntaxErrors and
the like.

HTH,
Marty
_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor





More information about the Tutor mailing list