Created
October 10, 2012 23:20
-
-
Save minrk/3869182 to your computer and use it in GitHub Desktop.
Example configuration file for using the IPython Notebook with a random password
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example configuration file for using the IPython Notebook with a random password | |
c = get_config() | |
import os | |
import base64 | |
from IPython.lib import passwd | |
password_chars = 8 # number of characters for the generated password | |
# generate n random bytes | |
b = os.urandom(password_chars) | |
# b64 encode, so it's ascii (and truncate, since b64 lengthens a bit) | |
password = base64.encodestring(b)[:password_chars] | |
# set the configurable to the hashed/salted value: | |
c.NotebookApp.password = passwd(password) | |
# Option 1. cleartext password to the terminal | |
print "Using password: %s" % password | |
# Option 2. write this to a private read-only file | |
# get the security dir, or put it wherever else you want: | |
from IPython.config import Application | |
security_dir = Application.instance().profile_dir.security_dir | |
# write the file | |
fname = os.path.join(security_dir, 'notebook_passwd') | |
with open(fname, 'w') as f: | |
f.write(password) | |
# make it private | |
import stat | |
os.chmod(fname, stat.S_IRUSR|stat.S_IWUSR) | |
# tell the terminal where it went: | |
print "Wrote password to private file: %s" % fname | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment