解析从请求发布接收到的多部分/表单数据 [英] parse multipart/form-data, received from requests post

查看:131
本文介绍了解析从请求发布接收到的多部分/表单数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am writing Web Service Client, using requests library. I am getting data in multipart/form-data that contains a file and text-json. I have no idea how to parse it. Is there a proper library to parse multipart/form-data format in python or should I write parser on my own?

我的代码:

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 \ n内容处置: 表格数据name ="playbackHash" \ r \ n内容类型: 应用程序/八位字节流\ r \ n \ r \ n \ x16 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00序列化:: archive \ n \ x00 \ x04 \ x08 \ x04 .... x00 \ x00R \ x94 \ x9bp \ x8c \ x00 \ r \ n--c00750d1-8ce4-4d29-8390-b50bf02a92cc \ r \ n内容处置: 表格数据name ="usersMFCC" \ r \ n内容类型: 应用程序/八位字节流\ r \ n \ r \ n \ x16 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00序列化:: archive \ n \ x00 \ x04 \ x08 \ x04 \ x08 \ x01 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ xf8 \ x16 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00u \ xbd \ xb4/\ xda1 \ xea \ xbf \ x0f \ xed \ xa2< \ xc9 \ xf8 \ xe7 \ xbf?\ xd5 \ xf06u \ xe7 \ xf0 \ xbf \ xd4 \ x8d \ xd4 \ xa1F \ xbe \ x03 @ \ x85X!\ x19 \ xd8A \ x06 @ \ x8co \ xf7 \ r .....
x80 \ xd9 \ x95Yxn \ xd0?\ r \ n--c00750d1-8ce4-4d29-8390-b50bf02a92cc \ r \ n内容处置: 表格数据name ="scoreAndStatus" \ r \ n内容类型:application/json; charset = utf-8 \ r \ n \ r \ n {"lexLikelihood":1.544479046897232,"overallScore":-nan,"playbackLikelihood":-inf,"status":{"errorCode":0,"errorMessage":"}} \ r \ n--c00750d1-8ce4-4d29-8390-b50bf02a92cc-\ r \ n'

b'\r\n--c00750d1-8ce4-4d29-8390-b50bf02a92cc\r\nContent-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\nContent-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\xed\xa2<\xc9\xf8\xe7\xbf?\xd5\xf06u\xe7\xf0\xbf\xd4\x8d\xd4\xa1F\xbe\x03@\x85X!\x19\xd8A\x06@\x8co\xf7\r .....
x80\xd9\x95Yxn\xd0?\r\n--c00750d1-8ce4-4d29-8390-b50bf02a92cc\r\nContent-Disposition: form-data; name="scoreAndStatus"\r\nContent-Type: application/json; charset=utf-8\r\n\r\n{"lexLikelihood":1.544479046897232,"overallScore":-nan,"playbackLikelihood":-inf,"status":{"errorCode":0,"errorMessage":""}}\r\n--c00750d1-8ce4-4d29-8390-b50bf02a92cc--\r\n'

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

I replaced more binary data with " ..... "

推荐答案

如果收到multipart/form-data响应,则可以使用requests-toolbelt库对其进行解析,如下所示:

If you're receiving a multipart/form-data response, you can parse it using the requests-toolbelt library like so:

$ 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)

这篇关于解析从请求发布接收到的多部分/表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆