[mod_python] large file upload using mod_python

Jeremy Jones zanesdad at bellsouth.net
Wed Aug 24 12:11:36 EDT 2005

Jeremy Jones wrote:

> I am writing a little piece of code that will handle potentially large 
> files, both for upload and download.  I tried the publisher handler, 
> but it looks like the publisher handler reads in the whole request and 
> then acts on it.  Since the file uploads (and downloads) may be really 
> huge, I thought it would be best to get all the HTTP headers and then 
> deal with the body of the POST.  Has anyone already done anything like 
> this?  It looks like I could create my own handler using a Connection 
> object, but if someone's already done the work and released the code, 
> no sense in reinventing fire.
> Thanks,
> Jeremy Jones
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python
OK - I think I solved this and it was *really* easy.  I set this in my 

<Directory /home/jmjones/public_html/file_upload>
        AddHandler mod_python .py
        PythonHandler upload
        PythonDebug On

And put this in a file called "upload.py" in my 
/home/jmjones/public_html/file_upload directory:

def handler(req):
    req.content_type = "text/plain"
    content_length = int(req.headers_in["Content-Length"])
    ret_str = ""
    outf = open("/tmp/upload_out.txt", "w")
    conn = req.connection
    i = 0
    ret_str += "\nbegin loop\n"
    while i < content_length:
        c = conn.read(1)
        if c == "":
        ret_str += "%s %s\n" % (i, c)
        i += 1
    ret_str += "end loop\n"
    ret_str += "closed file\n"
    return apache.OK

I wrote a script to spoof a browser uploading a file, a portion of which 
it did at one byte per second.  I opened the file /tmp/upload_out.txt as 
it was reading it from the client and it was updating it as the client 
would write a byte, so this appears to be doing exactly what I wanted 
with not much code.

mod_python rocks!

Jeremy Jones

More information about the Mod_python mailing list