Python - server side heterogeneous / form data analysis

I am trying to do http 'POST' using multipart / form-data on a Python based backend. My server-side method gets the full body, but I absolutely don't know how to analyze the contents of the body without manually sorting it and breaking the text into values.

My query looks like this:

POST /android/v4/MyPostMethod HTTP/1.1 Accept: */* Accept-Charset: * Content-Length: 186808 Content-Type: multipart/form-data; boundary=*****; charset="utf-8" Content_Length: 186808 Content_Type: multipart/form-data; boundary=***** Host: myhost.appspot.com User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.1.2; GT-I9300 Build/XXXXX) Via: HTTP/1.1 MSP-YV --***** Content-Disposition: form-data; name="value1" Content-Type: text/plain; charset=UTF-8 f0ef73c5-54dd-40cf-9ee7-5c4cb764eb28 --***** Content-Disposition: form-data; name="value2" Content-Type: text/plain; charset=UTF-8 10d71e73-4d4d-4607-b271-f7efcfd0c59d --***** Content-Disposition: form-data; name="value3" Content-Type: text/plain; charset=UTF-8 10d71e73-4d4d-4607-b271-f7efdfdfdfdf --***** Content-Disposition: form-data; name="logText"; filename="log.txt" Content-Type: text/plain Content-Transfer-Encoding: binary xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ... --*****-- 

I searched around and could not find a good explanation of how to do this trivially. Appreciate if someone can help me here. Thanks.

+5
source share
2 answers

For some reason, cgi.FieldStorage () did not work for me, but only the deprecated method:

 pdict = {'boundary':'*****'} cgi.parse_multipart(self.request.body_file, pdict) 

I donโ€™t know why, but as long as he works well with it.

+4
source

You need a .cgi python library.

In particular, something like this:

 import cgi form = cgi.FieldStorage() value1 = form.getfirst("value1", "") value2 = form.getfirst("value2", "") value3 = form.getfirst("value3", "") logtext = form.getfirst("logText", "") 
0
source

Source: https://habr.com/ru/post/1201732/


All Articles