2013-06-26 57 views
2

當我嘗試使用urllib2發送圖像時,UnicodeDecodeError異常發生。使用httplib POST二進制數據導致Unicode異常

HTTP POST正文:

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

任何人都知道一個解決的辦法?

+0

我不明白你在哪條線上發生異常 –

+0

@PauloBu我在opener.open(request)行得到了異常: – anasdox

回答

2

您正在嘗試發送包含標題和內容的正文。如果你想發送的內容類型和內容長度,你需要做的是在頭部,而不是在身體:

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

如果不設置Content-Length頭,它會自動設置爲的data

至於你的錯誤大小:它的發生就行

msg += message_body 

此錯誤時纔會發生,如果這兩個字符串中的一個是unicode,以及含有\xff其他str,如日如果後者將自動與使用sys.getdefaultencoding()的unicode相同。

我最後的猜測是:message_body這裏是你的data,這是一個str和包含\xff某處。 msg是之前傳遞給HTTPConnection的頭文件,它們是unicode,因爲您在標頭中至少使用了一個密鑰的unicode(之前的值轉換爲str),或者從__futures__導入了unicode_literals

+0

非常感謝,我避免了這樣的unicode異常: request = urllib2.Request(url.encode(「utf-8」),data = binary,headers = headers) 這是一個乾淨的方式來解決問題嗎? – anasdox

+0

只是不會使用Unicode或標頭的URL。網址應該採用百分比編碼,頭文件應該是iso-8859-1或根據[rfc 2047](http://tools.ietf.org/html/rfc2047#section-8)進行編碼。我最好的建議是:儘可能使用[requests](http://docs.python-requests.org/en/latest/),它在處理unicode方面更好。 – mata

+0

謝謝你的建議 – anasdox