Skip to content

Instantly share code, notes, and snippets.

@geoffalday
Created March 12, 2012 12:28
Show Gist options
  • Save geoffalday/2021517 to your computer and use it in GitHub Desktop.
Save geoffalday/2021517 to your computer and use it in GitHub Desktop.
How to generate a secret key with Python
# How to generate a secret key with Python
# via http://flask.pocoo.org/docs/quickstart/
import os
os.urandom(24)
@Dineshs91
Copy link

In python3.6 secrets module can be used to generate secrets.

>>> secrets.token_hex(16)  
'f9bf78b9a18ce6d46a0cd2b0b86df9da'

Example taken from the official documentation on secrets module.

@csolorio
Copy link

csolorio commented Nov 3, 2017

Can the secrets module be installed in python 2.x?

@floer32
Copy link

floer32 commented Feb 1, 2018

@csolorio I checked for a backport of Python 3.6 stdlib secrets and I couldn't find one. But if you look at cpython/secrets.py you may find it's a small enough module to backport on your own for your project. (Or just implement and test the same gist yourself.)

@Amanimasila
Copy link

Hello
Traceback (most recent call last):
File "manage.py", line 9, in
from solarpi.app import create_app
File "/home/amani/Desktop/new/solarpi/solarpi/app.py", line 10, in
from solarpi.settings import ProdConfig
File "/home/amani/Desktop/new/solarpi/solarpi/settings.py", line 7, in
class Config(object):
File "/home/amani/Desktop/new/solarpi/solarpi/settings.py", line 8, in Config
SECRET_KEY = os_env['SOLARPI_SECRET']
File "/home/amani/Desktop/new/.venv/lib/python2.7/UserDict.py", line 40, in getitem
raise KeyError(key)
KeyError: 'SOLARPI_SECRET'

and the following is my setting .py

mport os

os_env = os.environ

class Config(object):
SECRET_KEY = os_env['SOLARPI_SECRET']
APP_DIR = os.path.abspath(os.path.dirname(file)) # This directory
PROJECT_ROOT = os.path.abspath(os.path.join(APP_DIR, os.pardir))
BCRYPT_LOG_ROUNDS = 13
ASSETS_DEBUG = True
DEBUG_TB_ENABLED = True # Disable Debug toolbar
DEBUG_TB_INTERCEPT_REDIRECTS = True
CACHE_TYPE = 'simple' # Can be "memcached", "redis", etc.
SQLALCHEMY_TRACK_MODIFICATIONS = False

class ProdConfig(Config):
"""Production configuration."""
ENV = 'prod'
DEBUG = False
DB_NAME = 'app.db'
DB_PATH = os.path.join(Config.PROJECT_ROOT, DB_NAME)
SQLALCHEMY_DATABASE_URI = 'sqlite:///{0}'.format(DB_PATH)
DEBUG_TB_ENABLED = False # Disable Debug toolbar
SENTRY_DNS = os_env.get('SENTRY_DNS', None)

class DevConfig(Config):
"""Development configuration."""
ENV = 'dev'
DEBUG = True
DB_NAME = 'dev.db'
DB_PATH = os.path.join(Config.PROJECT_ROOT, DB_NAME)
SQLALCHEMY_DATABASE_URI = 'sqlite:///{0}'.format(DB_PATH)
DEBUG_TB_ENABLED = True
ASSETS_DEBUG = True # Don't bundle/minify static assets
CACHE_TYPE = 'simple' # Can be "memcached", "redis", etc.

class TestConfig(Config):
TESTING = True
DEBUG = True
SQLALCHEMY_DATABASE_URI = 'sqlite:///dev.db'
BCRYPT_LOG_ROUNDS = 1 # For faster tests
WTF_CSRF_ENABLED = False # Allows form testing

any idea how i can solve this

@vijayabaskarFD
Copy link

For Python 2.X and Python 3.X

>>> import binascii
>>> binascii.hexlify(os.urandom(24))
b'0ccd512f8c3493797a23557c32db38e7d51ed74f14fa7580'

Did you get any solution for this. I too facing the same issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment