unicode - 使用请求库 PUT 二进制数据

标签 unicode python-2.7 put python-requests

我需要创建一个只在服务器上上传文件的小型 WebDAV 客户端。

我发现“requests”库似乎很容易使用,但我无法正确使用它。

客户端应该传输二进制文件——所以我使用了下面的例子:

>>> url = 'http://IPADDR/webdav'
>>> files = {'report.xls': open('report.xls', 'rb')}

>>> r = requests.post(url, files=files)

来自 http://docs.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file .

对我来说它不起作用,我有以下错误:

File ".../site-packages/requests/packages/urllib3/connectionpool.py", line 260, in _make_request
  conn.request(method, url, **httplib_request_kw)
File ".../httplib.py", line 941, in request
  self._send_request(method, url, body, headers)
File ".../httplib.py", line 975, in _send_request
  self.endheaders(body)
File ".../httplib.py", line 937, in endheaders
  self._send_output(message_body)
File ".../httplib.py", line 795, in _send_output
  msg += message_body
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 147: ordinal not in range(128)

输入文件应该以某种方式编码吗? (我没有在“请求”文档中找到任何相关内容)。

最佳答案

经过一些调试,我确实发现了问题所在。

我能够通过删除脚本中的以下导入来解决问题:

from __future__ import unicode_literals

此导入似乎导致 unwanted string conversions在 urllib3(请求依赖)中。 应作者要求explained , 此问题应针对 urllib3 提出.

关于unicode - 使用请求库 PUT 二进制数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9289616/

相关文章:

unicode - Emacs:打印 UTF-8 缓冲区

python - 如何在 INI 文件中写入时删除空格 - Python

python-2.7 - 带有强制前导零的python科学记数法

rest - 通过调用 rest-api 更新 tfs 的 wiki 页面

RESTful API 设计 : should unchangable data in an update (PUT) be optional?

powershell - 如何在 PowerShell 字符串文字中对 Unicode 字符代码进行编码?

Ruby:如何仅从 unicode 文本中分解打印连字?

python - 将 30 分钟添加到从文本文件读取的时间并与当前时间进行比较

ruby-on-rails - 尝试使用 RestClient 进行发布或放置;收到 422 错误

python - Unicode 编码错误 'latin-1' 编解码器无法编码字符 '\u2019'