python - 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)
解决方案
text
是传递给hashing
方法的实际字符串。我猜你打算调用encode
它,而不是将它用作函数:
hash = hashlib.sha256(text.encode('utf-8'))
推荐阅读
- javascript - SyntaxError:节点 js 中出现意外的令牌“导出”错误
- c# - 格式化包含公式的 get/return
- installation - WIx Installer:成功安装后有什么方法可以自动关闭安装程序屏蔽
- jquery - 单个系列向下钻取到多个不同系列的 Highcharts
- python - python:用另一行替换打印行的文本
- c# - Specflow.Autofac:在构建容器后交换依赖项
- javascript - 使用 CJS 和 ESM 模块是否存在兼容性问题?
- authentication - 如何在一个应用程序中进行多用户登录?
- regex - 如何用“sed”或“awk”替换每行中第二次出现的空格?
- c++ - 如何将一个函数中的值用于主函数?