Using python for CGI output...

Mel Wilson mwilson at the-wire.com
Tue Dec 10 17:45:39 EST 2002


In article <pan.2002.12.10.18.17.53.590584 at anon.org>,
"Ben" <me at anon.org> wrote:
> [ ... ]
>i've written a bit of code (below), and the output only shows the print
>statement from my defined function, rather than including that output from
>where it was called.
>How can i get this code to output it's html, along with the output of any
>functions that are called along the way?
>
>-----------------------------
>#!/usr/bin/python
>print "Content-type: text/html\n\n"
>
>#set some page attributes
>bgc ="#A8ACBA"
>
>def helloWorld():
>        print "Hello there world!"

Here, you want `return "Hello there world!"` , instead of `print ...`.

   If you're going to do `"""+helloWorld()+"""` later on,
you're counting on the value returned by helloWorld .. not
on any I/O that gets done inside helloWorld.  As you coded
it, without any return statement, helloWorld actually
returns a reference to the object named 'None', and the
resulting error shuts down your program before you can see
the entire printout.

   For an interesting sideline, add a statement
`return " :-) "` after `print "Hello there world!"` .
The result will show you the order things are actually
getting done in.  You may need to view source in your
browser to see everything .. it depends.

>
>print """
><html>
><head><title>:welcome:</title></head>
><body bgcolor="""+bgc+""">
>        function:
>        """+helloWorld()+"""
></body>
></html>
>
>"""
>



        Regards.        Mel.



More information about the Python-list mailing list