【问题标题】:POST binary data using httplib cause Unicode exceptions使用 httplib POST 二进制数据会导致 Unicode 异常
【发布时间】:2013-06-26 16:59:49
【问题描述】:

当我尝试使用 urllib2 发送图像时,发生 UnicodeDecodeError 异常。

HTTP 帖子正文:

f = open(imagepath, "rb")
binary = f.read()
mimetype, devnull = mimetypes.guess_type(urllib.pathname2url(imagepath))

body = """Content-Length: {size}
Content-Type: {mimetype}

{binary}
""".format(size=os.path.getsize(imagepath),  
           mimetype=mimetype,
           binary=binary)

request = urllib2.Request(url, body, headers)
opener = urllib2.build_opener(urllib2.HTTPSHandler(debuglevel=1))
response = opener.open(request)
print response.read()

追溯:

   response = opener.open(request)
  File "/usr/local/lib/python2.7/urllib2.py", line 404, in open
    response = self._open(req, data)
  File "/usr/local/lib/python2.7/urllib2.py", line 422, in _open
    '_open', req)
  File "/usr/local/lib/python2.7/urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "/usr/local/lib/python2.7/urllib2.py", line 1222, in https_open
    return self.do_open(httplib.HTTPSConnection, req)
  File "/usr/local/lib/python2.7/urllib2.py", line 1181, in do_open
    h.request(req.get_method(), req.get_selector(), req.data, headers)
  File "/usr/local/lib/python2.7/httplib.py", line 973, in request
    self._send_request(method, url, body, headers)
  File "/usr/local/lib/python2.7/httplib.py", line 1007, in _send_request
    self.endheaders(body)
  File "/usr/local/lib/python2.7/httplib.py", line 969, in endheaders
    self._send_output(message_body)
  File "/usr/local/lib/python2.7/httplib.py", line 827, in _send_output
    msg += message_body
  File "/home/usertmp/biogeek/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 49: invalid start byte

python 版本 2.7.5

有人知道解决办法吗?

【问题讨论】:

  • 我不知道你在哪一行得到异常
  • @PauloBu 我在 opener.open(request) 行遇到异常

标签: python python-2.7 urllib2 httplib


【解决方案1】:

您正在尝试发送包含标题和内容的正文。如果要发送内容类型和内容长度,则需要在标头中进行,而不是在正文中:

headers = {'Content-Type': mimetype, 'Content-Length', str(size)}
request = urllib2.Request(url, data=binary, headers=headers)

如果不设置Content-Length头,会自动设置为data的大小

至于你的错误:它正在发生

msg += message_body

只有当这两个字符串之一是unicode,而另一个包含\xffstr 时才会发生此错误,因为在这种情况下,后者将使用sys.getdefaultencoding() 自动合并为unicode。

我的最终猜测是:message_body 这是你的data,它是一个str,在某处包含\xffmsg 是之前传递给 HTTPConnection 的内容,即标头,它们是 unicode,因为您在标头中至少对一个键使用了 unicode(值之前转换为 str),或者您已经导入unicode_literals 来自__futures__

【讨论】:

  • 非常感谢,我避免了这样的 unicode 异常:request = urllib2.Request(url.encode("utf-8"), data=binary, headers=headers) 这是一个干净的解决问题的方法?
  • 只是不要将 unicode 用于标题或 url。 url 应该是百分比编码的,并且 headers 应该是 iso-8859-1 或根据rfc 2047 编码。我最好的建议是:如果可能的话,使用requests,这样处理 unicode 会更好。