Python 请求 - 发布带有 multipart/form-data 的 zip 文件 [英] Python Requests - Post a zip file with multipart/form-data

查看:55
本文介绍了Python 请求 - 发布带有 multipart/form-data 的 zip 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在玩 Python Requests 模块,到目前为止它很有趣.

但是,我在尝试使用 multipart/form-data 发布 zip 文件时遇到了问题.

我正在使用 Digest 身份验证并且能够成功发布其他文件类型,例如.xls 等.

我正在使用以下方法创建发布请求:

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

这个错误并给出:

requests.exceptions.ConnectionError: HTTPConnectionPool(host='10.2.2.70', port=80): 最大重试次数超过 url:/plugin_install(由  引起:[Errno 32] 管道损坏)

我尝试过使用较小尺寸的 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 的演示:

<预><代码>>>>进口请求>>>fileobj = open('/tmp/test.zip', 'rb')>>>r = requests.post('http://httpbin.org/post', data={"mysubmit":"Go"}, files={"archive":("test.zip", fileobj)})>>>r<响应[200]>>>>r.json(){u'origin': u'217.32.203.188', u'files': {u'archive': u'data:application/zip;base64,'}, 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;边界=9aec1d03a1794177a38b48416dd4c811'},u'json':无,u'data':u''}

I'm playing around with the Python Requests module that has so far been a delight.

However, I've run into an issue whilst attempting to post a zip file using multipart/form-data.

I'm using Digest authentication and have been able to successfully post other file types e.g. .xls etc.

I'm creating a post request using:

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

This errors out and gives:

requests.exceptions.ConnectionError: HTTPConnectionPool(host='10.2.2.70', port=80): Max retries exceeded with url: /plugin_install 
(Caused by <class 'socket.error'>: [Errno 32] Broken pipe)

I've tried with smaller size zip files and changing the data/files values, and the same error occurs.

Am I missing something obvious?

Thanks for any light you can shed!

解决方案

As far as requests is concerned, there is no difference between a zip file and any other binary blob of data.

Your server is broken here; it is cutting of the connection when you send it a zip file. That is not something requests can do anything about.

You may want to test against http://httpbin.org/ when you run into problems like these; it is a testing service built by the author of the requests library.

Another tip: you don't need to read the whole file object into memory when sending. Just pass the object itself to requests instead:

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

Demo against 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 请求 - 发布带有 multipart/form-data 的 zip 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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