#!/usr/bin/env python # encoding: utf-8 HTACCESS = ''' AuthName Authentication AuthType Basic AuthUserFile %s Require valid-user ''' def htaccess(htpasswdpath): return HTACCESS % htpasswdpath def htpasswd(username, passwd): import md5 import time import crypt salt = md5.new(time.ctime()).hexdigest()[0:2] return ':'.join([username, crypt.crypt(passwd, salt)]) if __name__ == '__main__': import os import sys from getpass import getpass if not len(sys.argv) == 3: usage = 'usage: %s username passwd_file' print usage % sys.argv[0] else: passwd = getpass('Basic Auth Password: ') rpasswd = getpass('Retype Basic Auth Password: ') if passwd == rpasswd: def _write(path, s): fh = open(path, 'w') fh.write(s) fh.close() username, passwd_file = sys.argv[1], sys.argv[2] passwd_path = os.path.expanduser(passwd_file) _write('.htaccess', htaccess(passwd_path)) _write(passwd_path, htpasswd(username, passwd)) else: print '\nPasswords unmatched, abort.' sys.exit(1)