我是靠谱客的博主 优秀蜜粉,这篇文章主要介绍使用Python生成随机密码的示例分享,现在分享给大家,希望可以做个参考。

生成随机密码这件事情用python来干确实相当的方便,优美的string方法加上choice简直是绝配
make_password.py

复制代码
1
2
3
4
5
6
7
8
9
###简单几行代码执行即可生成记不住的字符串### $ python make_passwd.py DLrw9EiT Qs4Wm84q RQwl4L2L u9g0LgwW jHPtYdyU ...
复制代码
1
2
3
4
5
6
7
$ python make_passwd.py DLrw9EiT Qs4Wm84q RQwl4L2L u9g0LgwW jHPtYdyU ...

代码如下——注释比代码长

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/python #--coding:utf-8--# #------------------------------------------------------------------------------- # Name: make_passwd # # Author: LiuSha # # Created: 28/12/2014 # Copyright: (c) WDZJ-SA 2014 #------------------------------------------------------------------------------- from random import choice import string def Makepass(length=8, chars=string.letters+string.digits): return ''.join([choice(chars) for i in range(length)]) if __name__ == '__main__': for i in range(10): print Makepass() ##下例基本上就是这个小脚本的所有工作核心了,使用random模块的choice方法取string模块生成的字符串## >>> string.letters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' >>> string.digits '0123456789' >>> choice(string.digits) '4' >>> choice(string.letters) 'T' ##有关生成器可参考:http://www.ipython.me/python/python-generator.html## #!/usr/bin/python #--coding:utf-8--# #------------------------------------------------------------------------------- # Name: make_passwd # # Author: LiuSha # # Created: 28/12/2014 # Copyright: (c) WDZJ-SA 2014 #------------------------------------------------------------------------------- from random import choice import string def Makepass(length=8, chars=string.letters+string.digits): return ''.join([choice(chars) for i in range(length)]) if __name__ == '__main__': for i in range(10): print Makepass() ##下例基本上就是这个小脚本的所有工作核心了,使用random模块的choice方法取string模块生成的字符串## >>> string.letters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' >>> string.digits '0123456789' >>> choice(string.digits) '4' >>> choice(string.letters) 'T' ##有关生成器可参考:http://www.ipython.me/python/python-generator.html##

 
生成一些人似乎能好记一些的密码(Qs4Wm84q这种密码似乎除了复制粘贴没有别的选择,话说前年我使用shell生成的类似的密码给ldap做默认密码,我当时公司就真有员工把这样的密码背下来了,现在想想真心是厉害~~~)。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
##这样看起来是比上面的好记一点了吧,但需要提供一个字典文件## $ python make_dictpass.py 1 8 1 ipythosd $ python make_dictpass.py nahontchen chenyibfeo ipythoniue coreostche ... $ python make_dictpass.py 1 8 1 ipythosd $ python make_dictpass.py nahontchen chenyibfeo ipythoniue coreostche ...

代码如下

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/python #--coding:utf-8--# #------------------------------------------------------------------------------- # Name: make_dictpass # # Author: LiuSha # # Created: 28/12/2014 # Copyright: (c) WDZJ-SA 2014 #------------------------------------------------------------------------------- import random import string class passwd(): data = open('./word.txt').read().lower() def renew(self, n, maxmem=3): self.chars = [] for i in range(n): randspot = random.randrange(len(self.data)) self.data = self.data[randspot:] + self.data[:randspot] where = -1 locate = ''.join(self.chars[-maxmem:]) while where < 0 and locate: where = self.data.find(locate) locate = locate[1:] c = self.data[where+len(locate)+1] if not c.islower(): c = random.choice(string.lowercase) self.chars.append(c) def __str__(self): return ''.join(self.chars) if __name__ == '__main__': import sys ##如果带参数的话可以定义生成密码的次数,长度,追溯记录## if len(sys.argv) > 1: dopass = int(sys.argv[1]) else: dopass = 8 if len(sys.argv) > 2: length = int(sys.argv[2]) else: length = 10 if len(sys.argv) > 3: memory = int(sys.argv[3]) else: memory = 3 onepass = passwd() for i in range(dopass): onepass.renew(length,memory) print onepass
复制代码
1
2
3
4
5
6
7
8
9
##字典文件(可以是各种单词的组合)## $ cat word.txt chenyi itchenyi python ipython coreos coreos.me ipython.me
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/python #--coding:utf-8--# #------------------------------------------------------------------------------- # Name: make_dictpass # # Author: LiuSha # # Created: 28/12/2014 # Copyright: (c) WDZJ-SA 2014 #------------------------------------------------------------------------------- import random import string class passwd(): data = open('./word.txt').read().lower() def renew(self, n, maxmem=3): self.chars = [] for i in range(n): randspot = random.randrange(len(self.data)) self.data = self.data[randspot:] + self.data[:randspot] where = -1 locate = ''.join(self.chars[-maxmem:]) while where < 0 and locate: where = self.data.find(locate) locate = locate[1:] c = self.data[where+len(locate)+1] if not c.islower(): c = random.choice(string.lowercase) self.chars.append(c) def __str__(self): return ''.join(self.chars) if __name__ == '__main__': import sys ##如果带参数的话可以定义生成密码的次数,长度,追溯记录## if len(sys.argv) > 1: dopass = int(sys.argv[1]) else: dopass = 8 if len(sys.argv) > 2: length = int(sys.argv[2]) else: length = 10 if len(sys.argv) > 3: memory = int(sys.argv[3]) else: memory = 3 onepass = passwd() for i in range(dopass): onepass.renew(length,memory) print onepass
复制代码
1
2
3
4
5
6
7
8
9
##字典文件(可以是各种单词的组合)## $ cat word.txt chenyi itchenyi python ipython coreos coreos.me ipython.me

最后

以上就是优秀蜜粉最近收集整理的关于使用Python生成随机密码的示例分享的全部内容,更多相关使用Python生成随机密码内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(140)

评论列表共有 0 条评论

立即
投稿
返回
顶部