Mercurial > hg > config
comparison python/password.py @ 853:5e05c41337e3
Example: generate passwords
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 10 Dec 2017 17:18:29 -0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
852:565875bc93b7 | 853:5e05c41337e3 |
---|---|
1 #!/usr/bin/env python | |
2 | |
3 """ | |
4 password generator | |
5 """ | |
6 | |
7 import argparse | |
8 import random | |
9 import string | |
10 import sys | |
11 | |
12 | |
13 def generate_password(population=string.ascii_letters+string.digits, | |
14 length=12): | |
15 """ | |
16 returns a random password `length` characters long | |
17 sampled from `population` | |
18 """ | |
19 | |
20 return ''.join(random.sample(population, length)) | |
21 | |
22 | |
23 def main(args=sys.argv[1:]): | |
24 | |
25 # parse command line | |
26 parser = argparse.ArgumentParser(description=__doc__) | |
27 parser.add_argument('-l', '--length', dest='length', | |
28 type=int, default=12, | |
29 help="length of password to generate [DEFAULT: %(default)s]") | |
30 options = parser.parse_args(args) | |
31 | |
32 # print generated password | |
33 print (generate_password(length=options.length)) | |
34 | |
35 if __name__ == '__main__': | |
36 main() |