python - 属性错误: module 'secrets' has no attribute 'token_bytes'

标签 python flask flask-socketio python-socketio

我正在尝试设置一个简单的 Flask/socketio 应用程序,但是当我尝试运行它时,我收到此错误:

File "C:\Users\tompi\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask\app.py", line 2464, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\tompi\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask_socketio\__init__.py", line 45, in __call__
    return super(_SocketIOMiddleware, self).__call__(environ,
    return self.engineio_app.handle_request(environ, start_response)
    return self.eio.handle_request(environ, start_response)
  File "C:\Users\tompi\AppData\Local\Programs\Python\Python39\Lib\site-packages\engineio\server.py", line 379, in handle_request
    r = self._handle_connect(environ, start_response,
  File "C:\Users\tompi\AppData\Local\Programs\Python\Python39\Lib\site-packages\engineio\server.py", line 530, in _handle_connect
    sid = self.generate_id()
  File "C:\Users\tompi\AppData\Local\Programs\Python\Python39\Lib\site-packages\engineio\server.py", line 504, in generate_id
    secrets.token_bytes(12) + self.sequence_number.to_bytes(3, 'big'))
AttributeError: module 'secrets' has no attribute 'token_bytes'

我不明白为什么。有什么想法吗?

这是代码:

from flask import Flask, render_template
from flask_socketio import SocketIO, emit

app = Flask(__name__)

app.config['SECRET_KEY'] = 'abc'
socketio = SocketIO(app)


@app.route('/')
def index():
    return 'hello world'
    

if __name__ == '__main__':
    socketio.run(app, debug=True)

最佳答案

当我执行 black (代码格式化程序)时,即使没有参数,类似的事情也发生在我身上:

C:\GIT\myprogram>c:\Python310-64\python.exe -m black
Traceback (most recent call last):
  File "c:\Python310-64\lib\runpy.py", line 187, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "c:\Python310-64\lib\runpy.py", line 146, in _get_module_details
    return _get_module_details(pkg_main_name, error)
  File "c:\Python310-64\lib\runpy.py", line 110, in _get_module_details
    __import__(pkg_name)
  File "src\black\__init__.py", line 45, in <module>
  File "c:\Python310-64\lib\site-packages\black\files.py", line 34, in <module>
    from black.handle_ipynb_magics import jupyter_dependencies_are_installed
  File "src\black\handle_ipynb_magics.py", line 49, in <module>
AttributeError: module 'secrets' has no attribute 'token_hex'

我在当前目录中有一个名为 secrets.py 的文件,这触发了此问题。

关于python - 属性错误: module 'secrets' has no attribute 'token_bytes' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66846608/

相关文章:

python - BrainTree Scientific, Inc. 注入(inject)泵(型号 bs-8000)rs232 的串行命令

python - Google App Engine 试图访问开发数据

python - 使用 flask 的 socketio 扩展从线程发出

azure - Azure 应用服务上的 Flask Socket.io 400 错误代码

python - 扑克引擎文档

python - 从 pandas 数据框中的多个重复字符串创建列表

python-2.7 - 使用 Python 请求 session 时出现 SSL EOF 错误

python - 我可以在 @app.before_request 中为我的 flask 请求对象附加一个值并将其转发给端点 View 函数吗?

Python字符串转二维列表

javascript - 在 Flask SocketIO 聊天中设置动态聊天室