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

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

我写了下面的代码,但问题是我收到了一个错误 (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')  

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

你需要在hashlib.sha1(password.encode('utf-8')后面加一个括号,所以hexdigest().upper()就被调用了。

以下代码适用于我:

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

我和你一样 class 并且得到了同样的错误。括号放错了。

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