Using python for CGI output...

Dennis Lee Bieber wlfraed at ix.netcom.com
Tue Dec 10 17:07:05 EST 2002


Ben fed this fish to the penguins on Tuesday 10 December 2002 10:17 am:

> Hi,
> i have a site which was written in C, and now i'm learning python so i
> can rewrite it...
> 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?
> 
> thanks for reading!
> 
> -----------------------------
> #!/usr/bin/python
> print "Content-type: text/html\n\n"
> 
> #set some page attributes
> bgc ="#A8ACBA"
> 
> def helloWorld():
>         print "Hello there world!"
> 
> print """
> <html>
> <head><title>:welcome:</title></head>
> <body bgcolor="""+bgc+""">
>         function:
>         """+helloWorld()+"""
> </body>
> </html>
> 
> """
> 

        Try format strings, AND have your function RETURN a value:

>>> def helloWorld():
...     return "Hello there world!"
...
>>> print """
... <html>
... <head><title>Welcome</title></head>
... <body bgcolor="%s">
... function: %s
... </body>
... </html>
... """ % ("#A8ACBA", helloWorld())

<html>
<head><title>Welcome</title></head>
<body bgcolor="#A8ACBA">
function: Hello there world!
</body>
</html>


-- 
 > ============================================================== <
 >   wlfraed at ix.netcom.com  | Wulfraed  Dennis Lee Bieber  KD6MOG <
 >      wulfraed at dm.net     |       Bestiaria Support Staff       <
 > ============================================================== <
 >        Bestiaria Home Page: http://www.beastie.dm.net/         <
 >            Home Page: http://www.dm.net/~wulfraed/             <




More information about the Python-list mailing list