[mod_python] PSP content-type problem

Lee E. Brown Administrator at leebrown.org
Sun Sep 26 12:05:03 EDT 2004


From: "Kamil Niechajewicz" <kamil at nvstudio.pl>
To: "mod_python user mailing list" <mod_python at modpython.org>
Sent: Saturday, September 25, 2004 4:42 PM
Subject: Re: [mod_python] PSP content-type problem


> On Sat, 25 Sep 2004 17:33:11 +0200, Robert Fendt <rmfendt at web.de> wrote:
>
>> I would suggest changing it to what the publisher handler does (if this
>> does not break anything, but at least it seems to work for me), e.g.:
>
> yeah, i agree with you. this should be changed.
> i have different matter that concerns me. actually psp instantly
> does req.write while processing the template, so its possible that
> only part of the page will be displayed due some processing errors
> (like using not supported by 'vars = {}' key name etc.) when it
> occurs we get an ugly error inside our page that was partially
> displayed to the user. that is not a very pretty solution.

The problem of leaving a user staring at half of a 'broken' page had me 
worried as well.
I have found several 'low-tech' solutions to that, though, that work 
perfectly well with the Publisher Handler.
(My examples are simplistic for brevity.)

Solution 1:  Apache's SSI module can be used in a 'pseudo-PSP' manner:

Within the HTML document:
<h1>Welcome <!--#include virtual="myscript.py/supply_pagetitle" -->!<h1>

Within the script:
return 'to my Herpetology page'

When the script works, the user sees "Welcome to my Herpetology page!"
If the script fails to run and PythonDebug is Off, the user just sees 
"Welcome!" without realizing that anything is missing.

***

Solution 2: Use python's string formatting '%' function to insert data at 
specific point in an HTML document:

Within the HTML document:
<p>You are visitor number %s.  Welcome!<p>

Within the script:
docfile = open(htmlfile, 'r')
filestr = docfile.read()
return filestr % hitcounter()

***

Solution 3: Use Python's string replace function to replace a placeholder 
that also serves as a meaningful error message:

Within the HTML document:
<h1>Today's winning Lottery Number:<h1>
<p>(Server temporarily unavailable)</p>

Within the script:
docfile = open(htmlfile, 'r')
filestr = docfile.read()
return filestr.replace('(Server temporarily unavailable)', winning_number)

***

The overall strategy that I've been following is to 'fail gracefully' by 
building my HTML document templates so that they would make sense to the 
user even if the document template was the only thing returned.  In other 
words, the web site is augmented by the Python back-end instead of being 
completely dependant upon it. 



More information about the Mod_python mailing list