Hello Word in CGI
Curtis Jensen
cjensen at bioeng.ucsd.edu
Thu Jul 6 13:50:54 EDT 2000
Bjorn Pettersen wrote:
>
> 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>)
Sorry, I didn't say witch line. I took out the print line with the time
in it. So now it is a simple hello world script. By the way, is,
print 'Content-type: text/html\n\n'
just as good as they way you have it? I've tried both.
>
> Check that:
>
> - your script is executable (by everyone)
It is. permissions are 775
> - some webservers require a .cgi extension (a symbolic link to your .py
> will work)
I tried that and got the same errors
> - python is where you think it is, and is executable (again by
> everyone)
from the unix prompt, I can run helloworld.py and it prints fine.
Permissions on python are 755
> - your webserver is set up to execute cgi scripts.
In the same directory as helloword.py there are Pearl cgi scripts that
run fine.
> 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>'
We tried this and got the same problem.
Anything else? Thanks for the help so far.
--
Curtis Jensen
cjensen at bioeng.ucsd.edu
http://www-bioeng.ucsd.edu/~cjensen/
FAX (425) 740-1451
More information about the Python-list
mailing list