Skip to content

Instantly share code, notes, and snippets.

@minrk
Created October 10, 2012 23:20
Show Gist options
  • Save minrk/3869182 to your computer and use it in GitHub Desktop.
Save minrk/3869182 to your computer and use it in GitHub Desktop.
Example configuration file for using the IPython Notebook with a random password
# 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
print "Using password: %s" % password
print
# 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
print "Wrote password to private file: %s" % fname
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment