首页 > 解决方案 > hashlib "TypeError: 'str' 对象不可调用"

问题描述

我正在尝试在 python 中构建一个 Flask API,它以散列版本返回我发送到 API 的文本。但是在散列时出现以下错误:“TypeError:'str' object is not callable”

这是我的代码:

from flask import Flask, request
from flask_restful import Api, Resource
import hashlib

app = Flask(__name__)
api = Api(app)


class hashing(Resource):
  def get(self, text):
    hash = hashlib.sha256(text('utf-8'))
    text_hashed = hash.hexdigest()
    return {"data":text_hashed}

api.add_resource(hashing, "/hash/<text>")

if __name__ == "__main__":
    app.run(debug=True)

标签: python

解决方案


text是传递给hashing方法的实际字符串。我猜你打算调用encode它,而不是将它用作函数:

hash = hashlib.sha256(text.encode('utf-8'))

推荐阅读