我有这段代码,我使用的是python 3.7
def hash_password(password):
return bcrypt.hashpw(password.encode('utf8'), bcrypt.gensalt())
def credentials_valid(username, password):
with session_scope() as s:
user = s.query(User).filter(User.name.in_([username])).first()
if user:
return bcrypt.checkpw(password.encode('utf8'), user.password.encode('utf8'))
else:
return False
但当我尝试运行时,会出现以下错误:
return bcrypt.checkpw(password.encode('utf8'), user.password.encode('utf8'))
AttributeError: 'bytes' object has no attribute 'encode'
checkpw(password, hashed_password)函数的bcrypt接受编码输入
您的两个参数password和hash_password(如果是unicode格式)需要进行编码。这就是你所做的。
但是,您为函数提供的“password”参数似乎已经编码为Python解释器提供的AttributeError
查看此工作实现:
相关问题 更多 >
编程相关推荐