Python 我从哪里获得 Flask 的 SECRET_KEY?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34902378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:42:05  来源:igfitidea点击:

Where do I get a SECRET_KEY for Flask?

pythonflask

提问by Tendi

I am trying to set up Flask-Debugtoolbar, but I get the message "DebugToolBar requires a SECRET_KEY". Where do I get the secret key?

我正在尝试设置 Flask-Debugtoolbar,但收到消息“DebugToolBar 需要 SECRET_KEY”。我从哪里得到秘钥?

采纳答案by r-m-n

The secret keyis needed to keep the client-side sessions secure. You can generate some random key as below:

密钥是需要保持客户端会话安全。您可以生成一些随机密钥,如下所示:

>>> import os
>>> os.urandom(24)
'\xfd{H\xe5<\x95\xf9\xe3\x96.5\xd1\x01O<!\xd5\xa2\xa0\x9fR"\xa1\xa8'

Just take that key and copy/paste it into your config file

只需获取该密钥并将其复制/粘贴到您的配置文件中

SECRET_KEY = '\xfd{H\xe5<\x95\xf9\xe3\x96.5\xd1\x01O<!\xd5\xa2\xa0\x9fR"\xa1\xa8'

See Sessionsdocumentation

请参阅会话文档

回答by rezakamalifard

In order to use session in flask you need to set the secret key in your application settings. secret key is a random key used to encrypt your cookies and save send them to the browser.

为了在烧瓶中使用会话,您需要在应用程序设置中设置密钥。密钥是一个随机密钥,用于加密您的 cookie 并保存将它们发送到浏览器。

This error is because of this line in the Flask-Debugtoolbar code

这个错误是因为Flask-Debugtoolbar 代码中这一行

To fix this you just need to set a SECRET_KEYin your config file.

要解决此问题,您只需要SECRET_KEY在配置文件中设置一个。

app.config['SECRET_KEY'] = "Your_secret_string"

or if you have a config file just add below config to it:

或者,如果您有一个配置文件,只需将以下配置添加到其中:

SECRET_KEY = "Your_secret_string"

回答by Grey Li

Get the random string for secret key:

获取密钥的随机字符串:

Method 1:Use osin Python 2/3:

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

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

Method 2:Use uuidin Python 2/3:

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

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

Method 3:Use secretsin Python >= 3.6:

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

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

Method 4:Use osin Python 3:

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

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

Set secret key in Flask

在 Flask 中设置密钥

Method 1:Use app.secret_key:

方法一:使用app.secret_key

app.secret_key = 'the random string'

Method 2:Use app.config:

方法二:使用app.config

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

Method 3:Put it in your config file:

方法三:把它放在你的配置文件中:

SECRET_KEY = 'the random string'

回答by Prakashmm

**generate secret key ** open your cmd

**生成密钥** 打开你的cmd

import secrets

导入机密

secrets.token_hex(16)

secrets.token_hex(16)

you will find random characters

你会发现随机字符

put it in your config file

把它放在你的配置文件中

app.config['SECRET_KEY'] = "generated key"

app.config['SECRET_KEY'] = "生成的密钥"