如何在twisted.web 中完成不那么糟糕的文件上传?

发布于 2024-09-10 03:54:49 字数 444 浏览 4 评论 0原文

我搜索了又搜索,但似乎找不到以任何合理的方式将文件上传到我的 twins.web 应用程序的方法。

目前,将文件上传发布到资源会生成一个 request.args['file'] 变量,该变量是一个填充了文件内容的列表。我找不到获取有关文件的任何信息的方法:mime 类型、文件名、文件大小(除了仅获取 args['file'][] 中字符串的长度)等 。

我读到,twisted.web2 更擅长文件上传 但是我不知道它好多少,也不知道如何使用twisted.web2 来处理twisted.web 应用程序中的文件上传。

有什么建议吗?这让我疯狂地烦恼——哦,我查看了请求标头,并没有真正找到任何有意义的东西。如何获取有关使用 Twisted 上传文件的更多元信息?

另外,

我怎样才能从请求对象获取裸露的 HTTP 请求?是否可以?

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 application.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱她像谁 2024-09-17 03:54:49

这是一个老问题,但是快速搜索 stackoverflow 并没有找到类似的问题/答案,因此这里有一个使用 twisted.web2 进行文件上传的快速示例。

隐藏的表单变量 file_foo
文件上传变量,以显示 Twisted 如何将它们分开:

<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>

在您的 Resource.render() 方法中,以下是访问表单的方法
变量:

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.')

输出:

         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'

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'
感悟人生的甜 2024-09-17 03:54:49

我按照此处描述的方式进行操作:上传解决方案
该解决方案使用 cgi.FieldStorage 来解析有效负载。

还:
为了解析的目的,您需要 request.content 而不是 request[args]
正如您所看到的,结果与 web2 request.files 中的结果几乎相同。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文