2017-10-04 28 views
0

我目前在從服務器獲取散列時遇到問題。無法從服務器獲取散列表

TypeError: Unicode-objects must be encoded before hashing 

這裏是錯誤:

data = urllib2.urlopen(url) #request 
hash = hashlib.md5(data.read().decode('utf-8')).hexdigest() # 

其實我試圖取代解碼()的編碼(),但它給出了另一個錯誤:

AttributeError: 'bytes' object has no attribute 'encode' 

任何想法?

+1

只要刪除'.decode('utf-8')'。 –

+0

謝謝!我認爲解碼在這裏很重要,但它修復了我的錯誤 –

+0

只是爲了理解:'urlopen()'的'read()'返回字節。 'decode()'將把字節轉換成一個unicode字符串。而'md5()'需要字節作爲輸入。 –

回答

1

只要刪除.decode('utf-8')即可。

爲了解:read()urlopen()返回字節。 decode()會將字節轉換爲unicode字符串。而md5()需要字節作爲輸入。