[mod_python] Checking a PSP-generated response before starting to send it..?

Aaron Robinson aaron.robinson at mojoworld.com
Fri Jun 29 04:17:36 EDT 2007


Hi Graham,

Thanks for your responses - I did find some interesting emails based on the
search terms you mentioned; many of them elaborating on hacks I'd pondered
on but dismissed as being too "hacky" - as I'm not familiar with mod_python
I didn't want to settle for a hack when there was a better way, but it was
taking me a while to come to the realization that there wasn't one..

The hack I'm toying with at the moment seems very simple and with only one
limitation, being that you can't directly reference "req" in the PSP code,
although I believe you can use "psp.req" to get the desired effect if
needed..
Here it is:


	class WriteThingy(StringIO.StringIO):
		def write(self, string, flush=1):
	      	StringIO.StringIO.write(self, string)

	vars['req'] = WriteThingy()
	template.run(vars)
	html = vars['req'].getvalue()


The only problem I have with this is that it's written after scrutinizing
the internals of the .run() method, and based on knowledge of exactly which
order things happen in, which could change with future versions.

Please let me know if you can see any holes in this..

Thanks for putting in the feature request - I think it's a completely worthy
feature, and if implemented, I think there would be more room for further
enhancements in future...


<WARNING: Going off on a tangent ahead..>


Here's another little something that I'm wondering whether is possible or
not in PSP... Below is a sample of my own little scripting language - works
the same way as PSP but it's all written the long way rather than evaluating
python code. It's got a few advantages over PSP from what I can tell, but
I'm willing to throw this one away to get the increased speed of PSP -
anyway, here goes:


	<div>
		^if iFromUID == iUID^
			<b>You</b>
		^else^
			<b>^escape(sFromAlias)^</b>
		^endif^
		^*TRIM*^:
	</div>



Now the result ends up on the screen as either "You :" or "Bob :" (or
whatever sFromAlias was)..  It's actually supposed to be "You:" or "Bob:"
(ie: without the space) but because html renderers turn strings of
whitespace into a single space, I added the *TRIM* directive, which
basically just removes whitespace to the left and right of where it's placed
to achieve this while still retaining readable code..

Any way you know of to achieve this with PSP?
What made me think of it is that it'd be simple to add if the PSP class
manipulated the result as a string rather then writing it out directly..

Thanks for your help,
Aaron.



-----Original Message-----
From: Graham Dumpleton [mailto:graham.dumpleton at gmail.com] 
Sent: Friday, 29 June 2007 6:31 p.m.
To: Aaron Robinson
Cc: mod_python at modpython.org
Subject: Re: [mod_python] Checking a PSP-generated response before starting
to send it..?

Hmmm, those search terms may not have helped much. Try:

  http://www.modpython.org/pipermail/mod_python/2005-May/018230.html

instead.

Have also logged an issued for adding proper way of doing this since
it does come up occasionally.

  https://issues.apache.org/jira/browse/MODPYTHON-236

Graham

On 29/06/07, Graham Dumpleton <graham.dumpleton at gmail.com> wrote:
> On 29/06/07, Aaron Robinson <aaron.robinson at mojoworld.com> wrote:
> >
> >
> >
> >
> > For lack of any feedback on this, I've come up with 1/2 a solution
> > (possibly), but would still very much appreciate *any* input..
> >
> >
> >
> > I've written a output filter which read()'s all the input it gets, then
> > stores it in an attribute it creates on filter.req (and write()'s
nothing),
> > then when it reads None (end of stream) it can compare the total page to
the
> > last sent page and either write() the whole page (unmodified) or change
the
> > 'Last-Modified' date to the previous date + change the response code to
a
> > 304...  problem is that I don't know if it's possible for an output
filter
> > to change the response code..
> >
> >
> >
> > Any ideas on how to make an output filter change the response code, or
> > whether there are any holes in this plan - or preferably if there's a
way to
> > avoid this completely?
>
> Can't be done.
>
> I'm a bit busy to respond in detail on this at the moment, but do a
> search through the mailing list archives using the search box on
> www.modpython.org for 'publisher PSP'. This should pick up messages
> which talk about using PSP from publisher, thus giving you more
> control. There has also been past discussions on how to make PSP
> output to a string so one can process output like you want. Look for
> 'PSP request wrapper'.
>
> Graham
>
> > Thanks,
> >
> > Aaron.
> >
> >
> >
> >  ________________________________
> >
> >
> > From: mod_python-bounces at modpython.org
> > [mailto:mod_python-bounces at modpython.org] On Behalf Of
> > Aaron Robinson
> >  Sent: Wednesday, 27 June 2007 11:28 p.m.
> >  To: mod_python at modpython.org
> >  Subject: [mod_python] Checking a PSP-generated response before starting
to
> > send it..?
> >
> >
> >
> >
> > Hi all,
> >
> >
> >
> > I'm running Apache 2.2.4 on Win32 with mod_python 3.3.1 and Python
2.5.1.
> >
> > I'm completely new to Apache (and hence mod_python), but am reasonably
> > proficient with python itself.
> >
> >
> >
> > I'm working on a web service where all content is generated on the fly,
and
> > am wanting to use PSP to facilitate this.
> >
> > Making as much use as possible of "304: Not Modified" is extremely
important
> > to this project to reduce bandwidth, which is difficult as I have to
> > generate the content before I know whether it's different ("been
modified")
> > from the last time I sent it or not..
> >
> >
> >
> > I previously used a mechanism (not using Apache) where I would generate
the
> > response, then take a hash of it and if it matched the hash of the last
> > response I sent, I would instead send a "304: Not Modified".
> >
> > My problem is that when using PSP.run(), the response has already been
sent
> > by the time it's finished being generated, so I have no opportunity to
step
> > in and examine the response to see if sending a 304 is possible.
> >
> >
> >
> > I'm needing an across-the-board mechanism for dealing with this - I
thought
> > of serializing the "vars" dictionary before passing it in and taking a
hash
> > of that for comparison, but this is hardly full-proof, as it would
> > (incorrectly) lead to responding with a 304 even with different
"session"
> > information, or, in the slightly odd case that the PSP file made use of
> > random.random(), etc.
> >
> >
> >
> > Just a thought: Is there a more general mechanism that can detect
identical
> > content to a previous response on the way out and replace it with a 304?
> >
> >
> >
> > Thanks in advance,
> >
> > Aaron.
> > _______________________________________________
> > Mod_python mailing list
> > Mod_python at modpython.org
> > http://mailman.modpython.org/mailman/listinfo/mod_python
> >
> >
>



More information about the Mod_python mailing list