【问题标题】:TypeError: object supporting the buffer API required in flask apiTypeError:支持flask api中所需的缓冲区API的对象
【发布时间】:2018-11-11 19:25:22
【问题描述】:

我有这段代码,我想将密码转换成 md5

class UserLogin(Resource):
    def post(self):

            # Parse the arguments

            parser = reqparse.RequestParser()
            parser.add_argument('username')
            parser.add_argument('password')
            args = parser.parse_args()

            _user = args['username']
            _userPassword = args['password']
            _h = hashlib.md5(_userPassword.encode())
            conn = mysql.connect()
            cursor = conn.cursor()
            cursor.execute('''select * from user where username = %s && password = %s''', (_user, _h))
            data = cursor.fetchall()

            return jsonify(data)

但错误提示:h = hashlib.md5(_userPassword.encode()) AttributeError: 'NoneType' 对象没有属性 'encode'

当我从 hashlib 中删除 encode() 时,错误返回是:_h = hashlib.md5(_userPassword) TypeError:需要支持缓冲区 API 的对象

请帮助我。我使用python3.6

【问题讨论】:

    标签: python python-3.x api flask


    【解决方案1】:

    而不是

    _h = hashlib.md5(_userPassword.encode())
    

    你可能想要

    _h = hashlib.md5(_userPassword.encode()).hexdigest()
    

    如果您要存储密码的 MD5 哈希,则

    password = md5(%s)
    

    如果绑定_h,部分查询将无法匹配。

    【讨论】:

    • 对不起,我忘了编辑它,因为密码 = md5(%s)''',(_user,_userPassword) 也不起作用。这就是为什么我需要使用 hashlib 并且您的答案仍然返回错误说没有属性“编码”感谢您的回答。
    • 如果您收到“无属性”错误,则您省略了括号。但如果... password = md5(%s)", (..., _userPassword)) 不起作用,您可能会遇到更深层次的问题。
    • 是的,戴夫先生,我已经发现了问题所在。 :) 它在我的系统上。 :) 我重新安装了我的操作系统,查询与 md5(%s) 一起工作。非常感谢。你给了我一些想法,它有一个更深层次的问题,我重新安装了 ubuntu :) 并且它解决了。再次感谢。抱歉,我回复晚了:)
    猜你喜欢
    • 2019-05-25
    • 2021-10-15
    • 2016-11-05
    • 2021-05-16
    • 2022-01-21
    • 2015-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多