如何在twisted.web 中完成不那么糟糕的文件上传?
我搜索了又搜索,但似乎找不到以任何合理的方式将文件上传到我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个老问题,但是快速搜索 stackoverflow 并没有找到类似的问题/答案,因此这里有一个使用
twisted.web2
进行文件上传的快速示例。隐藏的表单变量
file_foo
与文件上传变量,以显示 Twisted 如何将它们分开:
在您的
Resource.render()
方法中,以下是访问表单的方法变量:
输出:
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 afile upload variable, to show how Twisted will split these out:
In your
Resource.render()
method, here's how you could access the formvariables:
Output:
我按照此处描述的方式进行操作:上传解决方案。
该解决方案使用 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
notrequest[args]
.As you can see, the results are almost the same as in web2
request.files
.