AttributeError: 'bytes' 对象没有属性 'hexdigest'

时间:2021-04-17 19:27:05

标签: python-3.x hex hashlib

我编写了以下代码,但问题是我收到一个错误 (AttributeError: 'bytes' object has no attribute 'hexdigest') 错误语法不起作用

import requests
import hashlib 

def request_api_data (query_char):
    url = 'https://api.pwnedpasswords.com/range/'+ query_char
    res = requests.get(url)
    if res.status_code != 200:
        print('it is an error')
        #raise RuntimeError(f'Error fetching: {res.status_code}, check api and try again')
        return res
request_api_data('123')

def pwned_api_check(password):
    sha1password= hashlib.sha1(password.encode('utf-8').hexdigest().upper())
    
    print (sha1password)

    #return sha1password

pwned_api_check('123')  

为什么会出现这个错误,我该如何解决??

1 个答案:

答案 0 :(得分:0)

您需要在 hashlib.sha1(password.encode('utf-8') 后添加一个括号,以便在其上调用 hexdigest().upper()

以下代码对我有用:

hashlib.sha1(password.encode('utf-8')).hexdigest().upper()