Hello Word in CGI
Curtis Jensen
cjensen at bioeng.ucsd.edu
Thu Jul 6 15:35:01 EDT 2000
Bjorn Pettersen wrote:
>
> Curtis Jensen wrote:
> >
> > 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.
>
> print 'Content-type: text/html\n\n' is fine...
>
> Hmmm... it seems clear that there is something going wrong when the
> webserver is trying to execute your script (the errormessage being
> "premature end of headers")...
>
> How about
>
> #!/bin/sh
> echo "Content-type: text/html"
> echo
> echo "<h1>hello world</h1>"
>
> if that works, create a text file called yyy.txt and make it world
> writable (777), then try:
>
> #!/bin/sh
> echo "Content-type: text/html"
> echo
> /usr/local/httpd/cgi-bin/test_jmsun/cont_forms/helloworld.py >> yyy.txt
> 2>&1
>
> Your browser should tell you that the document contained no data, but
> the yyy.txt file will hopefully contain the error... (you're getting
> very close to the limit of my cgi knowledge here ;-)
>
> -- bjorn
The shell-cgi worked fine. I redirected the output and the yyy.txt file
remained empty. If I ran the shell-cgi script from the unix prompt,
yyy.txt contained the correct html code.
So, even though I didn't get any errors in the yyy.txt file, this helped
me figure out what is going on. The webserver is not able to execute
the python executable. Python was compiled for a newer OS than what the
webserver is running at. I was running the scripts from my machine
which is IRIX 6.5, but the OS on the webserver is older. I logged into
the webserver, and tried running the python interpreter and it returned
with a new unix prompt. So, that's our problem. We're going to have to
make a work around for this. Thanks for the help.
--
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