开发者

How can I accomplish file uploads in twisted.web that don't suck?

开发者 https://www.devze.com 2023-01-07 08:32 出处:网络
I\'ve searched and searched but can\'t seem to find a way to upload files to my twisted.web application in any reasonable way.

I've searched and searched but can't seem to find a way to upload files to my twisted.web application in any reasonable way.

Currently, posting file uploads to a resource results in a request.args['file'] variable, that is a list populated with file contents. I can't find a way to get any information about the file: mime type, filename, filesize (other than just taking the length of the strings in args['file'][]), etc.

I have read that twisted.web2 is better at file uploads. However I don't know how much better it is, or how I would use twisted.web2 to handle file uploads in a twisted.web appli开发者_JS百科cation.

Any suggestions? This is bugging me like crazy -- Oh and I looked at the request headers, and didn't really find anything of any significance. How can I get some more meta information about file uploads with Twisted?

Also,

How can I just get the bare HTTP request from a request object? Is it possible?


This is an old question, but a quick search of stackoverflow didn't turn up a comparable question/answer, so here is a quick example of using twisted.web2 for file uploads.

The hidden form variable file_foo shares the same name as a file upload variable, to show how Twisted will split these out:

<form action="/upload?a=1&b=2&b=3" enctype="multipart/form-data"
        method="post">
    <input type="hidden" name="foo" value="bar">
    <input type="hidden" name="file_foo" value="not a file">
    file_foo: <input type="file" name="file_foo"><br/>
    file_foo: <input type="file" name="file_foo"><br/>
    file_bar: <input type="file" name="file_bar"><br/>
    <input type="submit" value="submit">
</form>

In your Resource.render() method, here's how you could access the form variables:

def render(self, ctx):
    request = iweb.IRequest(ctx)
    for key, vals in request.args.iteritems():
        for val in vals:
            print key, val

    print 'file uploads ----------------'
    for key, records in request.files.iteritems():
        print key
        for record in records:
            name, mime, stream = record
            data = stream.read()
            print '   %s %s %s %r' % (name, mime, stream, data)

    return http.Response(stream='upload complete.')

Output:

         a: 1
         b: 2 3
       foo: bar
  file_foo: not a file

file_bar
   bar.txt MimeType('text', 'plain', {}) <open file '<fdopen>', mode 'w+b' at 0x2158a50> 'bar data.\n\n'
file_foo
   foo.txt MimeType('text', 'plain', {}) <open file '<fdopen>', mode 'w+b' at 0x2158930> 'foo data.\n\n'
   foo.txt MimeType('text', 'plain', {}) <open file '<fdopen>', mode 'w+b' at 0x21589c0> 'foo data.\n\n'


I did it like it is described here: solution for upload. The solution uses cgi.FieldStorage to parse the payload.

Also: For the purpose of parsing you need request.content not request[args]. As you can see, the results are almost the same as in web2 request.files.

0

精彩评论

暂无评论...
验证码 换一张
取 消