如何在twisted中完成文件上传?网络不吸?

时间:2021-03-09 20:56:13

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.

我找了又找,但似乎找不到上传文件到我的扭曲。web应用程序以任何合理的方式。

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.

目前,将文件上传到资源会导致请求。args['file']变量,这是一个包含文件内容的列表。我找不到一种方法来获取关于文件的任何信息:mime类型、文件名、filesize(除了在args['file'][]][]]中获取字符串的长度等。

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.

我读过那个扭曲的故事。web2更擅长文件上传。但是我不知道它有多好,或者我该如何使用twisted。web2处理在一个扭曲的文件上传。web应用程序。

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?

如何从请求对象中获得裸HTTP请求?是可能的吗?

2 个解决方案

#1


4  

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

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

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

隐藏的表单变量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>

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

在您的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.')

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'

#2


3  

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

我按照这里描述的那样做:上传解决方案。这个解决方案使用cgi。FieldStorage用于解析有效负载。

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.

同样:为了解析,需要请求。内容不请求(args)。如您所见,结果几乎与web2 request.files中的结果相同。

#1


4  

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

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

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

隐藏的表单变量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>

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

在您的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.')

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'

#2


3  

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

我按照这里描述的那样做:上传解决方案。这个解决方案使用cgi。FieldStorage用于解析有效负载。

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.

同样:为了解析,需要请求。内容不请求(args)。如您所见,结果几乎与web2 request.files中的结果相同。