[Tutor] Exception and sys.exit() in a cgi script

wesley chun wescpy at gmail.com
Mon Oct 23 19:34:46 CEST 2006


> > > 7 print '<html><br><br><body><p>Please enter values in the
> > > fields</p></body></html> '
> > >
> > > When the excption occurs, no message is shown on the browser.
> > > If I run the script with IDLE, the message is printed and then the script exits.
> >
> >
> > import cgitb; cgitb.enable()
> > That spits errors to the web page which is great for debugging cgi
> > scripts.
> >
> > Thats not the point (sorry).
> > when all the values are provided in the form (no excetion is raised) it
> runs as expected!
> >
> > The point is when one of the values "ano", "conta" is missing in the form,
> I want the error message to be shown on the browser, and the script to stop
> running.
> >
> > When there is an exception, the script actually stops running, but no
> message is sent to the browser.


paulino,

mike and michael are correct.  i know your script runs fine when the
form fields are entered.  the output is quite different tho.  as alan
and everyone has alluded to, sending output to the screen is different
than sending to a browser there is "more work" in order to get the
text to it.

when you display to the screen, it is a direct connect to the standard
output (not nec stdout) display, which is your monitor.  when you send
things to a browser, you have to remember that web pages flow on the
network (even on the same machine).  the protocol that is used is
called HTTP, and as the writer of software that runs on the server
side, you need to communicate to the client using that protocol
properly to the client (otherwise that defeats the purpose of even
*having* a protocol).

the headers which michael has mentioned in his above post are the
required part of the HTTP protocol that must come before any HTML is
sent (by your script).  review the recommended documents to get a good
idea of how CGI works with HTTP and HTML, and then you will be ok.

here is a Core Tip i placed on p.882 in the web programming chapter my
book (see below):

CORE TIP: HTTP headers separate from HTML
One thing that always nails CGI beginners is that when sending
results back to a CGI script, it must return the appropriate HTTP
headers first before any HTML. Furthermore, to distinguish between
these headers and the resulting HTML, several NEWLINE characters
must be inserted between both sets of data, as in line 5 of our
friends1.py example, as well as for the code in the remaining part of
the chapter.

if you happen to have access to a copy of the book, that chapter will
really help you really understand how to write a CGI script
successfully. basically, my summary is this: you will see a lot of
500/ISE errors and cgitb is your friend.  :-)

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
    http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com


More information about the Tutor mailing list