解析从发布请求中收到的multipart / form-data

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (361)

我正在使用请求库编写Web服务客户端。我在multipart / form-data中获取包含文件和text-json的数据。我不知道如何解析它。是否有一个适当的库来解析python中的multipart / form-data格式,还是应该自己编写解析器?

我的代码:

data = {
  "prototypeModel" :('prototypeModel', open(prototypeModel, 'rb'), 'application/octet-stream', {'Expires': '0'}),
  "mfcc_1" : ('mfcc', open(mfcc_1, 'rb'), 'application/octet-stream', {'Expires': '0'}),
  "mfcc_2" : ('mfcc', open(mfcc_2, 'rb'), 'application/octet-stream', {'Expires': '0'}),
  "mfcc_3" : ('mfcc', open(mfcc_3, 'rb'), 'application/octet-stream', {'Expires': '0'}),
}

print( '---------------------- start enroll ----------------------')
testEnrollResponse = requests.post(server+sessionID, files = data, json = declaredParameters)

b'\ r \ n - c00750d1-8ce4-4d29-8390-b50bf02a92cc \ r \ nConContent-Disposition:form-data; name =“playbackHash”\ r \ nContent-Type:application / octet-stream \ r \ n \ r \ n \ x16 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00serialization :: archive \ n \ x00 \ x04 \ x08 \ x04 .... x00 \ x00R \ x94 \ x9bp \ x8c \ x00 \ r \ n - c00750d1-8ce4-4d29-8390-b50bf02a92cc \ r \ nConContent-Disposition:form-data; name =“usersMFCC”\ r \ nContent-Type:application / octet-stream \ r \ n \ r \ n \ x16 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00serialization :: archive \ n \ x00 \ x04 \ X08 \ X04 \ X08 \ X01 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ XF8 \ X16 \ X00 \ X00 \ X00 \ X00 \ X00 \ x00u \ XBD \ XB4 / \ xda1 \ XEA \ XBF \ X0F \固定的\ XA2 <\ xc9 \ XF8 \ XE7 \ XBF?\ XD5 \ xf06u \ XE7 \ XF0 \ XBF \ XD 4 \ x8d \ XD 4 \ xa1F \ XBE \ X03 @ \ x85X!\ X19 \ xd8A \ X06 @ \ x8co \ xf7 \ r ..... x80 \ xd9 \ x95Yxn \ xd0?\ r \ n - c00750d1-8ce4-4d29-8390-b50bf02a92cc \ r \ nConContent-Disposition:form-data; name =“scoreAndStatus”\ r \ nConContent-Type:application / json; 字符集= UTF-8 \ r \ n \ r \ N { “lexLikelihood”:1.544479046897232 “overalScore”: - “playbackLikelihood”: - INF, “状态”:{ “errorCode”:0 “的errorMessage”:” “}} \ r \ N - c00750d1-8ce4-4d29-8390-b50bf02a92cc - \ r \ N”

我用“.....”替换了更多的二进制数据

提问于
用户回答回答于

如果您收到multipart/form-data,可以使用requests-toolbelt库解析它,如下所示:

$ pip install requests-toolbelt

安装后

from requests_toolbelt.multipart import decoder

testEnrollResponse = requests.post(...)
multipart_data = decoder.MultipartDecoder.from_response(testEnrollResponse)

for part in multipart_data.parts:
    print(part.content)  # Alternatively, part.text if you want unicode
    print(part.headers)
用户回答回答于

Flask的代码示例,使用https://github.com/defnull/multipart

import multipart as mp
from multipart import tob

try:
    from io import BytesIO
except ImportError:
    from StringIO import StringIO as BytesIO

@app.route('/', methods=["GET","POST"])
def index():
        ...
        elif flask.request.method == "POST":
                data = flask.request.data
                s = data.split("\r")[0][2:]
                p = mp.MultipartParser(BytesIO(tob(data)),s)
                blob = p.parts()[0].value
                f = open("file.bin","wb")
                f.write(blob.encode("latin-1"))
                f.close()

扫码关注云+社区

领取腾讯云代金券