python - 'bytes' 对象没有属性 'encode'

标签 python python-3.x bcrypt

在将每个文档插入集合之前,我试图存储盐和散列密码。但是在对盐和密码进行编码时,它显示以下错误:

 line 26, in before_insert
 document['salt'] = bcrypt.gensalt().encode('utf-8')

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

这是我的代码:

def before_insert(documents):
    for document in documents:
        document['salt'] = bcrypt.gensalt().encode('utf-8')
        password = document['password'].encode('utf-8')
        document['password'] = bcrypt.hashpw(password, document['salt'])

我在 virtualenv 中使用 python 3.4 中的 eve 框架

最佳答案

您正在使用:

bcrypt.gensalt()
此方法似乎生成一个字节对象。这些对象没有任何编码方法,因为它们只适用于 ASCII 兼容数据。所以你可以不用.encode('utf-8')

试试

Bytes description in python 3 documentation

关于python - 'bytes' 对象没有属性 'encode',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38246412/

相关文章:

python - 与 0 进行比较以检查 Python 和 C 中的条件

python - 根据另一列更改 pandas 列

python - 在 for 循环中保存 scikit-learn 模型

python - 无法获取给出错误结果的网页

javascript - 使用 bcryptjs 进行密码哈希处理

node.js - Heroku 部署失败,似乎是 bcrypt 问题

ruby-on-rails - 带有 BCrypt 的 Rails Fixtures

javascript - 运行带有Ajax问题的Python

python - 尝试使用 pip3.4 在 CentOS 5.11 服务器中安装 python 模块 (beautifulsoup) 时出错

python - 在 Python 中从空格分隔的 key=value 字符串创建字典