Fwd: [mod_python] mod_python: handling file uploads

Graham Dumpleton graham.dumpleton at gmail.com
Sat Mar 24 20:30:03 EST 2007


Sorry, I stuffed up this time and didn't send it back to the list. :-(

---------- Forwarded message ----------
From: Graham Dumpleton <graham.dumpleton at gmail.com>
Date: 25-Mar-2007 11:25
Subject: Re: [mod_python] mod_python: handling file uploads
To: "Ruben K." <midreth at gmail.com>


On 25/03/07, Ruben K. <midreth at gmail.com> wrote:
> I built a simple website framework that is not compatible with the
> publishing handler, now I am looking to build a feature into that framework
> to handle uploads.
>
> While researching on my own how to do this I found that mod_python's
> documention is very lacking
> in this area.
>
> I have the following questions:
>
> - In which part of mod_python's instance (usually named req or request) is
> the uploaded file information found?
> - How would I go about moving temporary files to the target directories?
> Would I want to open() a file with w+ or something (create if doesn't exist)
> and write() the temporary file's content into it?
> - Is there a default handler for actions like this?

Have you read the following part of the documentation:

http://www.modpython.org/live/current/doc-html/pyapi-util-fstor.html
http://www.modpython.org/live/current/doc-html/pyapi-util-fstor-examples.html#pyapi-util-fstor-examples

The FieldStorage class which is what is used to decode forms and
handle file uploads has a file_callback constructor argument which can
be used to control how uploaded files are handled and where they are
initially stored.

This at least is one way of doing things such that it is handled
within mod_python.

For a more interesting example which you may be able to adapt, have a
look at Tramline (http://www.infrae.com/products/tramline). This is an
input filter than can intercept file uploads and save them away such
that mod_python response handlers don't have to worry about them. All
that gets passed through to a response handler is a file ID of some
sort which it might then use to refer to the file and do further
processing on it.

Graham


More information about the Mod_python mailing list