简体   繁体   English

Python请求 - 发布带有multipart / form-data的zip文件

[英]Python Requests - Post a zip file with multipart/form-data

I'm playing around with the Python Requests module that has so far been a delight. 我正在使用迄今为止令人高兴的Python Requests模块。

However, I've run into an issue whilst attempting to post a zip file using multipart/form-data. 但是,我在尝试使用multipart / form-data发布zip文件时遇到了问题。

I'm using Digest authentication and have been able to successfully post other file types eg .xls etc. 我正在使用摘要式身份验证,并且能够成功发布其他文件类型,例如.xls等。

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. 我尝试使用较小的zip文件并更改数据/文件值,并发生相同的错误。

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. requests而言,zip文件和任何其他二进制blob数据之间没有区别

Your server is broken here; 你的服务器坏了; it is cutting of the connection when you send it a zip file. 当你发送一个zip文件时它正在切断连接。 That is not something requests can do anything about. 这不是requests可以做任何事情的事情。

You may want to test against http://httpbin.org/ when you run into problems like these; 当你遇到这些问题时,你可能想要对http://httpbin.org/进行测试; it is a testing service built by the author of the requests library. 它是由requests库的作者构建的测试服务。

Another tip: you don't need to read the whole file object into memory when sending. 另一个提示:发送时不需要将整个文件对象读入内存。 Just pass the object itself to requests instead: 只需将对象本身传递给requests

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 : 针对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''}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM