2017-10-04 27 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()需要字节作为输入。