[Tutor] Using python for building dynamic websites

Lloyd Kvam pythontutor at venix.com
Mon Feb 9 10:48:09 EST 2004


I have not used mod_python, so I can't help you on that front.  There are
alternatives.  cgi and fast-cgi are relatively easy to do in Python.
The downside to cgi is the extra overhead of starting a process and making
a new database connection for every request.  However, for many applications
this works fine.

The cgi module can be used to collect form data.
You need to provide the content header typically:
print "Content-Type: text/html\n"
The headers MUST be followed by a blnk line.

owlfish.com provides a standalone version of the ZopePageTemplates.  There
are other template systems available.  The Python string interpolation using
the dictionary lookup syntax also works well for generating html.

If you prefer fast-cgi, the jonpy modules provide a simple framework for fast-cgi
scripting.

This is my little function for sending static files to the browser.  Note that
sys.stdout is normally connected back to the web server in cgi programs so that
it is simply a matter of copying the file contents to sys.stdout.

def copy_snippet( filename, out_file=None):
     if out_file is None:
         out_file = sys.stdout
     if os.path.exists( filename):
         snip = open( filename, 'r')
         shutil.copyfileobj( snip, out_file)
         snip.close()


Obviously, I have opted for a looser coupling with Apache than mod_python provides.
You'll need to figure out what makes the most sense for your application.  I hope
this helps.

RenderPipe wrote:

> Hello,
> 
> Yesterday I was looking around all of the options for building a database
> driven website with python and my head is spinning with all of the
> options. I installed mod_python on my linux computer and ran the test
> script in the manual and it works although it doesn't appear as straight
> forward as I thought. I'm starting to really like python and really don't
> want to go back to php unless I have to. 
> 
> Here's what I would like to do.
> 
> Write my code in python.
> Call a template file
> Output the data from my python and template file to the browser
> Some areas of the site I would like to output static html files from my
> python/template code.
> 
> I have no interest in learrning another language or complex framework
> like zope to do this. I would prefer to use just python. The problem I'm
> having is that this doesn't seem possible for me. From what I understand
> of mod_python I have to write special handlers for every script that I
> write. I may be understanding it wrong. 
> 
> I tried something like:
> 
> for i in range(1,10):
>     print i,
> #req.write() gave me undefined errors
> 
> I saved this file as num.py. when i load this file in my webbrowser
> http://localhost/num.py. I get an error from the publisher.
> In the end I just want to build my site in a straight forward manner. I
> didn't bother with all the other frameworks like pmz because I'm not
> allowed to install software on the host server, I can only use the
> preinstalled mod_python.
> 
> If it is too difficult to use python, then I was thinking of using php to
> interact with my database instead. I read somewhere that python can be
> used as a "backend" to a php site. What does this mean? Can you possibly
> give a scenario where python would be used as a "backend".
> 
> Sorry for all of the web related questions but I'm madly confused. My
> whole purpose to learn python was mainly for building data driven
> websites with ease of modification.
> 
> TIA
> 
> Bobby
> 

-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-653-8139
fax:	801-459-9582




More information about the Tutor mailing list