【问题标题】:AttributeError: 'bytes' object has no attribute 'hexdigest'AttributeError: 'bytes' 对象没有属性 'hexdigest'
【发布时间】:2021-04-17 19:27:05
【问题描述】:

我编写了以下代码,但问题是我收到了一个错误 (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 error: AttributeError: 'bytes' object has no attribute 'hexdigest'”

标签: python-3.x hex hashlib


【解决方案1】:

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

以下代码适用于我:

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

【讨论】:

    【解决方案2】:

    我和你上同一门课,遇到了同样的错误。括号放错地方了。

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

    【讨论】:

    • 这与接受的答案中的解决方案相同。在回答已有答案的旧问题时,请确保您提供新颖的解决方案或比现有答案更好的解释。请记住首先查看所有现有答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    • 2012-12-01
    • 2021-04-19
    • 2021-11-22
    相关资源
    最近更新 更多