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

Michael P. Reilly arcege at gmail.com
Sun Oct 22 15:06:51 CEST 2006


On 10/21/06, Paulino <paulino1 at sapo.pt> wrote:
>
>
>  *Mike Hansen*
>     Mike.Hansen at atmel.com
>         <tutor%40python.org?Subject=%5BTutor%5D%20Exception%20and%20sys.exit%28%29%20in%20a%20cgi%20script&In-Reply-To=>
>
>     *Mon Oct 16 18:43:29 CEST 2006*
> >* This is a peace of a CGI script i have.
> *>*
> *>* 1 import cgi
> *>* 2 form=cgi.FieldStorage()
> *>* 3 try :
> *>* 4     ano=form["ano"].value
> *>* 5     conta=form["conta"].value
> *>* 6 except KeyError :
> *>* 7     print '<html><br><br><body><p>Please enter values in the
> *>* fields</p></body></html> '
> *>* 8     sys.exit(0)
> *>*
> *>*
> *>* 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.
> *>*
> *>* What's wrong here?
> *>*
> *>*
> *>*
> *>* _______________________________________________
> *>* Tutor maillist  -  Tutor at python.org <http://mail.python.org/mailman/listinfo/tutor>*>* http://mail.python.org/mailman/listinfo/tutor*>* *
>
> I'm catching up on the weekend's tutor messages.
>
> import cgitb; cgitb.enable()
> That spits errors to the web page which is great for debugging cgi
> scripts.
>
> I think you need
> print "Content-Type: text/html\n\n"
> http://docs.python.org/lib/cgi-intro.html
> http://docs.python.org/lib/node560.html
>
> Mike
>
>
> 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.
>

Actually, this is the point.  The first lines of output from your CGI
script, even on an error, needs to be header information to the web server
and to the web browser, then a blank line, and then the content to be
displayed (text, html, image, etc.).  If you don't include the "print
'Content-type: text/html\n'" line that Mike suggests, then your HTML will be
swallowed by the system as configuration information (and erroneous
information at that).

Mike suggested some webpages to review.  It might be good to pour over them
to learn more about the CGI protocols.

  -Arcege

-- 
There's so many different worlds,
So many different suns.
And we have just one world,
But we live in different ones.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061022/91ccb19d/attachment.htm 


More information about the Tutor mailing list