【问题标题】:How to get secret key in process of connecting flask app to database如何在将烧瓶应用程序连接到数据库的过程中获取密钥
【发布时间】:2019-10-07 00:15:59
【问题描述】:

我正在关注使用 json 在 Flask 和 PostgreSQL 之间建立数据库连接的教程,并且有一个密钥,在 config.py 中提到

我已经阅读了一些其他的答案并理解 Flask 使用 urandom 来生成随机密钥。但不完全清楚我必须在什么时候运行此代码来生成密钥。我明白这段代码必须在命令提示符下运行。

python
    >>> import os
    >>> os.urandom(24)

我的 config.py 代码

import os
basedir = os.path.abspath(os.path.dirname(__file__))

class Config(object):
    DEBUG = False
    TESTING = False
    CSRF_ENABLED = True
    SECRET_KEY = 'this-really-needs-to-be-changed'
    SQLALCHEMY_DATABASE_URI = os.environ['postgresql://postgresql:silverTip@localhost/DatabaseFirst']

class ProductionConfig(Config):
    DEBUG = False

class StagingConfig(Config):
    DEVELOPMENT = True
    DEBUG = True

class DevelopmentConfig(Config):
    DEVELOPMENT = True
    DEBUG = True

class TestingConfig(Config):
    TESTING = True

【问题讨论】:

    标签: python postgresql flask flask-sqlalchemy


    【解决方案1】:

    在 python shell 中运行该代码:

    >>> import os
    >>> os.urandom(24)
    b'\x1d\xc6\x0f[\xed\x18\xd6:5\xe0\x0f\rG\xaf\xb4\xf4HT\xef\xc1\xf1\xa89f'
    

    然后将结果复制/粘贴到您的配置文件中:

    SECRET_KEY = '\x1d\xc6\x0f[\xed\x18\xd6:5\xe0\x0f\rG\xaf\xb4\xf4HT\xef\xc1\xf1\xa89f'
    

    记得删除前导的b,否则你会将SECRET_KEY保存为字节对象,而不是字符串。

    【讨论】:

      猜你喜欢
      • 2019-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-28
      • 2015-04-13
      • 1970-01-01
      相关资源
      最近更新 更多