Creating temperory files for a web application

Stephen Hansen apt.shansen at gmail.com
Fri May 8 16:36:52 CEST 2009


>
>
> Thank you Diez.
> I would like to go ahead with the cleanest solution indeed.
> I am creating the image on the fly. But I could not understand what
> you meant by render to memory as a string.
> How do we send the image to the browser?
>
> Were you mentioning about having the image as a string and then
> passing to the browser based on data URL scheme <RFC 2397> ?
> Or is it something else like XBM format or something? I am sorry - but
> I am quite unsure of what you meant?
>
> I am actually creating PDFs, CSVs and images on the fly. So, I need to
> delete them all after that.
> The images can be somewhat large - ~2MB. The RFC mentions that large
> data cannot be sent that way.
>

The question is, how are you generating these images? What you consider a
"file" and what a web server considers a "file" is really meaningless
actually. To a web browser, all a "file" is is a stream of content with a
certain content-type which it uses to determine how to treat it. This isn't
about the RFC2397. The web browser has no idea if the content its fetching
is from a file or a database or generated on the fly-- its all the same
thing. There's a URL which returns data, when requested. That data might be
a HTML file (as determined by its Content-type), in which case the browser
renders it. Or it might be a JPG, in which case it displays it. Or it might
be a zip file in which case it probably asks you where you want to save it.

Depending on how exactly you're actually "generating" the image, its quite
possible you don't need to use a real file at all. You can use a file-like
object, such as (c)StringIO and pass it to your image generation routines
which will generate the entire image in memory. You can write to this
file-like object as if it were a real file on the disk somewhere, seek,
scan, pass it to most libraries that need file objects.... and then when
done, you can return its contents... all without a -real- file on disk ever
being touched.

Like so vaguely:
    f = cStringIO.StringIO()
    my_library_which_makes_images(f)
    f.seek(0)
    return f.read()

Exactly how you indicate what the Content-type will be and do that will
depend on what web framework you're using.

For example, on one app I have -- I'm using TurboGears -- I do something
like this:

    @expose(format="image/jpeg", content_type="image/jpeg")
    def getJPEG(self, element_number):
        element = self._resourceManager.getElement(element_number)
        if element:
            return element.data

        return ''

Now the specifics of what's going on aren't important. But I'm connecting to
a remote system, and getting an object which contains the entire contents of
a JPEG in a single string. So the entire file is in memory. I could then
tweak or manipulate it with PIL to change that file all without any
filesystem access, working entirely on file-like objects in memory, then
return the contents.

--S
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090508/e7674433/attachment.html>


More information about the Python-list mailing list