[Twisted-web] large file upload in web2 (was Re: http.HTTPFactory(site) equivalent in web2.0.2?)

Lenny G Arbage alengarbage at yahoo.com
Tue Jun 27 10:52:39 CDT 2006


> Did you create a bug about it when you found it in
> 0.1? If not could you?  Because I didn't know about
> any such issue, and I'm guessing neither did
> anyone else. Assign it to me and I'll look into it
> this evening after work (any code you have to
> reproduce such an issue would be helpful it doesn't
> necessarily have to be in the form of a unittest
> but if you can do that it'd be awesome.)

  I just found it yesterday (in 0.1) and thought I
should upgrade to 0.2 before bugging anyone about it. 
I'd be happy to create a bug, but I have no idea how
to do that ("TICKET_CREATE privileges are required to
perform this operation", and I haven't been able to
get myself over the wall of "where are the
instructions on getting an account or gaining those
privileges?")

  Here, though, is a simple example illustrating the
problem:

#!/usr/bin/python

import os
from twisted.internet import reactor
from twisted.web2 import server, http, resource,\
    channel
from twisted.web2 import http_headers, responsecode
from twisted.web2 import iweb

SAVEDIR = "/tmp"
READSIZE=8192

class UploadFile(resource.PostableResource):
    def render(self, ctx):
        request = iweb.IRequest(ctx)
        filename = request.files['filename'][0][0]
        file = request.files['filename'][0][2]

        dest = os.path.join(SAVEDIR, filename)
        destfile = open(dest, 'wb')
        tot = 0
        while True:
            buf = file.read(READSIZE)
            tot = tot + READSIZE
            print "reading %d..." % tot
            if buf == '':
                break
            destfile.write(buf)
        destfile.close()
        file.close()

        msg = "saved %s to %s" % (filename, dest)
        print msg
        return http.Response(stream="%s" % msg)

class Toplevel(resource.Resource):
    addSlash = True
    def render(self, ctx):
        return http.Response(responsecode.OK,
                {'content-type':
http_headers.MimeType('text', 'html')},
                "Hello")

    child_uploadfile = UploadFile()

if __name__ == "__main__":
    site = server.Site(Toplevel())
    reactor.listenTCP(1080, channel.HTTPFactory(site))
    reactor.run()


  Run this script, then upload a file to it with the
client of your choice.  Here is an html form which you
can use to do that:

<html>
    <form action="http://localhost:1080/uploadfile"
            enctype="multipart/form-data"
            method="post">
        filename: <input type="file" name="filename">
        <input type="submit" value="submit">
    </form>
</html>

  You should be able to upload files smaller than 9M
without any problem, and files larger than 10M not at
all.

  I can send you an httplib client as well, but at
least this should be enough to get you started.

  I'll go try again, now, to figure out how to get
this filed properly in Trac (and maybe even as a
unittest!).

  Thanks!
  Lenny

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



More information about the Twisted-web mailing list