Hello Word in CGI

Bjorn Pettersen bjorn at roguewave.com
Thu Jul 6 13:47:33 EDT 2000


Curtis Jensen wrote:
> 
> Bjorn Pettersen wrote:
> >
> > What you're seeing are the symptoms of Python having thrown an exception
> > and the weblog not capturing all the information. The solution is to make
> > sure Python can only throw where you can grab the exception and do
> > something useful with it, like printing it to the browser...  Try the
> > following (but make sure you can execute it from the command line first --
> > one possible error is a SyntaxError, which this scheme will not save you
> > from...)
> >
> > -- bjorn
> >
> > #!/usr/bin/env python
> > print """Content-type: text/html
> >
> > """
> >
> > def main():
> >     from time import *
> >
> >     print "<HTML>"
> >     print "<Head>"
> >     print "<Title>Hello World</Title>"
> >     print "</Head>"
> >     print "<Body>"
> >     print "<H1>Hello World !</H1>"
> >     print "<hr>"
> >     print "<P>This is San Diego.<br>Localtime is", ctime( time() ) + ".<P>"
> >
> >     print "</Body>"
> >     print "</HTML>"
> >
> > import traceback
> >
> > def execute(fn):
> >     # make sure any errors are legible, and directed to the user.
> >     try:
> >         sys.stderr = sys.stdout
> >         fn()
> >     except:
> >         print '<h1>Something went wrong!!!</h1>'
> >         print 'Please send the transcript below to webmaster at your.web<p>'
> >         print '\n\n<pre>'
> >         traceback.print_exc()
> >
> > execute(main)
> >
> 
> I tried this.  I got the same errors in the log and in the browser.  I
> took out the print line since it caused a discussion.  Any more
> suggestions?  Thanks.

You do need the print line! The first thing your cgi must print out
(modulo cookies) is "Content-type: text/html" followed by exactly two
newlines (yes, I know there are situations where this isn't true, but in
this case it is.. <wink>)

Check that:

 - your script is executable (by everyone)
 - some webservers require a .cgi extension (a symbolic link to your .py
will work)
 - python is where you think it is, and is executable (again by
everyone)
 - your webserver is set up to execute cgi scripts.

If all that works, try the simplest possible cgi script, something like:

#!/usr/local/bin/python
print  """Content-type: text/html

"""
print '<h1>hello world</h1>'

hth, 
-- bjorn




More information about the Python-list mailing list