通用传入文件上传所有python框架的http请求对象 [英] Generic incoming file upload http request object for all python frameworks
问题描述
我正在使用一个SDK来验证并将输入的multipart / form-data文件保存到服务器端的磁盘上。如下:
I am working on a SDK that validates and saves incoming multipart/form-data files to disk on the server side. Something like:
sdk.upload(httpRequest, destinationPath, validationOptions)
我需要传入的http请求:字段名称,文件名,文件和MIME类型。
I need the incoming http request for: field name, file name, file and mime type.
问题是python框架有不同的http请求对象:
The problem is that python frameworks have different http request objects:
- Django: HttpRequest
request.FILES
包含文件 - Flask:请求
request.files
包含文件。 - 金字塔:
request.POST
包含文件。
- Django: HttpRequest
request.FILES
contains the files - Flask: request
request.files
contains the files. - Pyramid:
request.POST
contains the files.
从文件中的文件值对象相同
字典,具有不同的结构。
Same for a file value object from files
dictionary, which has different structures.
我需要的是一个统一的/通用的http请求对象或另一个解决方案
What I need is a unified/generic http request object or another solution to handle that in one way.
可能不方便的解决方案:
Possible inconvenient solutions:
- 有每个框架的不同实现:我想以通用的方式处理它。
- 使用lib解析多部分/表单数据:它实际上将是一个重新解析,因为框架已经解析了一次
其他语言的示例:
- PHP:$ _FILES对象
- NodeJS:可读流请求
- ASP.NET:static HttpContext.Current.Request
推荐答案
所有框架都实现WSGI协议 https://www.python.org/dev/peps/pep-0333/ 。这是共同的基础机制,他们已经建立了自己的便利功能。您可以随时返回原始的WSGI。
All frameworks implement WSGI protocol https://www.python.org/dev/peps/pep-0333/ . It's the common underlying mechanism and they have built their own convenience functions on the top of that. You can always go back to raw WSGI.
上传过程后,存在一个通用的存储和处理文件的框架:Depot http://depot.readthedocs.io/en/latest/
After upload has been processes there is a common framework to store and process the file: Depot http://depot.readthedocs.io/en/latest/
例如,您可以在每个框架中获取原始的WSGI数据并生成WebOb Request对象。 http://webob.org/
For example you can grab raw WSGI data and generate WebOb Request object out of it in every framework http://webob.org/
另请参阅Authomatic的灵感 http:// peterhudec.github.io/authomatic/
Also see Authomatic for inspirations http://peterhudec.github.io/authomatic/
这篇关于通用传入文件上传所有python框架的http请求对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!