-
-
Save sandervm/2b15775012685553f0e2 to your computer and use it in GitHub Desktop.
$ python -c 'import random; print "".join([random.choice("abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)") for i in range(50)])' |
tr -dc 'a-z0-9!@#$%^&*(-_=+)' < /dev/urandom | head -c50
For Python 3, this should work:
python -c 'import random; result = "".join([random.choice("abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)") for i in range(50)]); print(result)'
I think you should use parenthesis while using print
method in Python 3
python -c 'import random; print("".join([random.choice("abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)") for i in range(50)]))'
Seems exceedingly unlikely the random
package contains a sufficiently secure random.
In Linux you can read from /dev/urandom or /dev/random(slower) and see someone suggesting random.SystemRandom
which is probably more portable.
base64 /dev/urandom | head -c50
Docker edition which utilizes django default secret key generation:
docker run --rm python:3 bash -c "pip install django; django-admin startproject foo; echo; cat foo/foo/settings.py | grep SECRET_KEY"
docker run --rm python:3 bash -c "pip install django; python -c 'from django.core.management.utils import get_random_secret_key; get_random_secret_key()'"
Using Mr. Ioa's suggestion I was able to generate a secret key. However when I ran again 'docker-compose down", it threw this error
ERROR: environment variable name 'SECRET_KEY ' may not contains whitespace.
Any suggestion how to fix that.
@halfprisoner 'SECRET_KEY ' there is a trailing white space in your env variable
Also, how did you store the random key generated above into a variable before passing it as an env variable in the docker run of your project?
Лучше бы ты написал как из SECRET_KEY сгенерировать куку
This is how django does it internally, translated into bash...
KEY=$(chars='abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'; python -c "import secrets; print(''.join(secrets.choice('${chars}') for i in range(50)))")
For Python 3, this should work:
python -c 'import random; result = "".join([random.choice("abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)") for i in range(50)]); print(result)'