Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic incoming file upload http request object for all python frameworks [closed]

Tags:

python

wsgi

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
like image 980
Florin Marius Popescu Avatar asked Aug 14 '26 02:08

Florin Marius Popescu


1 Answers

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/

like image 75
Mikko Ohtamaa Avatar answered Aug 16 '26 17:08

Mikko Ohtamaa