您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python请求-发布包含multipart / form-data的zip文件

Python请求-发布包含multipart / form-data的zip文件

requests目前而言,zip文件与任何其他二进制数据块之间 。

您的 服务器 在这里坏了;当您发送一个zip文件时,它切断了连接。那是requests无能为力的。

http://httpbin.org/当您遇到此类问题时,您可能需要进行测试;它是requests库作者构建的测试服务。

一个提示:发送时不需要将整个文件对象读入内存。只需将对象本身传递给requests

fileobj = open('/Users/.../test.zip', 'rb')
r = requests.post(url, auth=HTTPDigestAuth('dev', 'dev'), data = {"mysubmit":"Go"}, files={"archive": ("test.zip", fileobj)})

针对的演示httpbin.org

>>> import requests
>>> fileobj = open('/tmp/test.zip', 'rb')
>>> r = requests.post('http://httpbin.org/post', data={"mysubmit":"Go"}, files={"archive": ("test.zip", fileobj)})
>>> r
<Response [200]>
>>> r.json()
{u'origin': u'217.32.203.188', u'files': {u'archive': u'data:application/zip;base64,<long base64 body omitted>'}, u'form': {u'mysubmit': u'Go'}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'57008', u'Accept-Encoding': u'gzip, deflate, compress', u'Connection': u'close', u'Accept': u'*/*', u'User-Agent': u'python-requests/1.2.3 cpython/2.7.5 Darwin/12.4.0', u'Host': u'httpbin.org', u'Content-Type': u'multipart/form-data; boundary=9aec1d03a1794177a38b48416dd4c811'}, u'json': None, u'data': u''}
python 2022/1/1 18:46:06 有328人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶