生成随机密码这件事情用python来干确实相当的方便,优美的string方法加上choice简直是绝配,有关random可以看看我之前的拙文~~~~
make_password
###简单几行代码执行即可生成记不住的字符串###Mr_Liu:great_script$ python make_passwd.py DLrw9EiTQs4Wm84qRQwl4L2Lu9g0LgwWjHPtYdyU...
代码如下——注释比代码长——
#!/usr/bin/python#--coding:utf-8--##-------------------------------------------------------------------------------# Name: make_passwd## Author: LiuSha## Created: 28/12/2014# Copyright: (c) WDZJ-SA 2014#-------------------------------------------------------------------------------from random import choiceimport stringdef 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做默认密码,我当时公司就真有员工把这样的密码背下来了,现在想想真心是厉害~~~)。
##这样看起来是比上面的好记一点了吧,但需要提供一个字典文件##Mr_Liu:great_script$ python make_dictpass.py 1 8 1ipythosdMr_Liu:great_script$ python make_dictpass.py nahontchenchenyibfeoipythoniuecoreostche...
代码如下:
#!/usr/bin/python#--coding:utf-8--##-------------------------------------------------------------------------------# Name: make_dictpass## Author: LiuSha## Created: 28/12/2014# Copyright: (c) WDZJ-SA 2014#-------------------------------------------------------------------------------import randomimport stringclass 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 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##字典文件(可以是各种单词的组合)##Mr_Liu:great_script$ cat word.txt chenyiitchenyipythonipythoncoreoscoreos.meipython.me
? 转载保留版权:IT辰逸 ? 《Python 生成随机密码》 ? 本文链接地址:http://www.ipython.me/python/python-make-password.html ? 本文版权采取:BY-NC-SA 协议进行授权,转载注明出处。除IT-Tools、News以及特别标注,本站所有文章均为原创。 ? 如果喜欢可以:点此订阅本站
成功是什么?就是走过了所有通向失败的路.只剩下一条路.那就是成功的路.