gpt4 book ai didi

python - 我从哪里获得 Flask 的 SECRET_KEY?

转载 作者:IT老高 更新时间:2023-10-28 22:02:59 66 4
gpt4 key购买 nike

在尝试设置 Flask-Debugtoolbar 时,我得到:

"DebugToolBar requires a SECRET_KEY".

我从哪里获得 SECRET_KEY

最佳答案

获取 key 的随机字符串:

方法一:在Python 2/3中使用os:

>>> import os
>>> os.urandom(12)
'\xf0?a\x9a\\\xff\xd4;\x0c\xcbHi'

方法2:在Python 2/3中使用uuid:

>>> import uuid
>>> uuid.uuid4().hex
'3d6f45a5fc12445dbac2f59c3b6c7cb1'

方法3:在Python中使用secrets >= 3.6:

>>> import secrets
>>> secrets.token_urlsafe(16)
'Drmhze6EPcv0fN_81Bj-nA'
>>> secrets.token_hex(16)
'8f42a73054b1749f8f58848be5e6502c'

方法4:在Python 3中使用os:

>>> import os
>>> os.urandom(12).hex()
'f3cfe9ed8fae309f02079dbf'

在 Flask 中设置 key

方法一:使用app.secret_key:

app.secret_key = 'the random string'

方法二:使用app.config:

app.config['SECRET_KEY'] = 'the random string'    

方法 3: 放到你的配置文件中:

SECRET_KEY = 'the random string'

然后加载配置表单配置文件:

app.config.from_pyfile('config.py')  # if your config file's name is config.py

关于python - 我从哪里获得 Flask 的 SECRET_KEY?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34902378/

66 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com