[mod_python] Image handling handler

Damjan gdamjan at mail.net.mk
Thu Sep 23 20:31:24 EDT 2004


>>> I'm probably missing something obvious here, but in the 'handle_thumb'
>>> and 'handle_size' class methods if the image is not cached (or is older)
>>> where is the method sending the image file back to the client? 
>>> It appears only to resize and cache the image not return it.
>> 
>> img.save(self.req, format=img.format)
>> 
>> I'm "saving" the image to the req object. mod_python's request object is
>> fine at emulating a file object.
>
> I'm probably having one of those dumb moments (It happens often!) but I
> just don't get this.

:)
 
> How does the request object know its supposed to be a file object and
> relay the image to the browser.  

The request object is "file object" compatible, that means that any
python function that works with file objects can work with the request
object too. Like this:

def fill_file_with_zeros(something, howmany):
	buff = '\x0' * howmany
	something.write(buff)
	return

Now I can call that function like this:

fp = file('/tmp/zeroed')
fill_file_with_zeros(fp, 100)

or in mod_pytnon like this:

fill_file_with_zeros(req, 100)

The function doesn't care if "something" is really a file. Its enough if
"something" has the write method.
So in the second case, the function fill_file_with_zeros calls "req"s
write method... what happens is that the client receives a file filled with
NULLs (well, that's not very usefull ;) ).

> When/where does the req.write get invoked to send the image?

Inside img.save(...), img.save is just like the above fill_file_with_zeros,
it doesn't care if you give it a real file, or a file like object (like
req)... as long as it supports the write method.

Btw, I could've done it like this:

	buff = StringIO.StringIO()
	img.save(buff,...)
	req.write(buff.getvalue())

But that would only use twice the memory for no reason.

-- 
damjan | дамјан
This is my jabber ID --> damjan at bagra.net.mk <-- not my mail address!!!


More information about the Mod_python mailing list