问题
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)
I need the incoming http request for: field name, file name, file and mime type.
The problem is that python frameworks have different http request objects:
- 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.
What I need is a unified/generic http request object or another solution to handle that in one way.
Possible inconvenient solutions:
- Have different implementations for each framework: I want to handle it in a generic way.
- Parsing the multipart/form-data with a lib: It will actually be a re-parsing as the framework has already parsed it once.
Examples in other languages:
- PHP: $_FILES object
- NodeJS: Readable stream request
- ASP.NET: static HttpContext.Current.Request
回答1:
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.
After upload has been processes there is a common framework to store and process the file: Depot http://depot.readthedocs.io/en/latest/
For example you can grab raw WSGI data and generate WebOb Request object out of it in every framework http://webob.org/
Also see Authomatic for inspirations http://peterhudec.github.io/authomatic/
来源:https://stackoverflow.com/questions/39647719/generic-incoming-file-upload-http-request-object-for-all-python-frameworks