使用Python生成隨機(jī)密碼的示例分享
生成隨機(jī)密碼這件事情用python來干確實(shí)相當(dāng)?shù)姆奖悖瑑?yōu)美的string方法加上choice簡(jiǎn)直是絕配
make_password.py
###簡(jiǎn)單幾行代碼執(zhí)行即可生成記不住的字符串### $ python make_passwd.py DLrw9EiT Qs4Wm84q RQwl4L2L u9g0LgwW jHPtYdyU ...
$ python make_passwd.py DLrw9EiT Qs4Wm84q RQwl4L2L u9g0LgwW jHPtYdyU ...
代碼如下——注釋比代碼長(zhǎng)
#!/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() ##下例基本上就是這個(gè)小腳本的所有工作核心了,使用random模塊的choice方法取string模塊生成的字符串## >>> string.letters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' >>> string.digits '0123456789' >>> choice(string.digits) '4' >>> choice(string.letters) 'T' ##有關(guān)生成器可參考: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() ##下例基本上就是這個(gè)小腳本的所有工作核心了,使用random模塊的choice方法取string模塊生成的字符串## >>> string.letters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' >>> string.digits '0123456789' >>> choice(string.digits) '4' >>> choice(string.letters) 'T' ##有關(guān)生成器可參考:http://www.ipython.me/python/python-generator.html##
生成一些人似乎能好記一些的密碼(Qs4Wm84q這種密碼似乎除了復(fù)制粘貼沒有別的選擇,話說前年我使用shell生成的類似的密碼給ldap做默認(rèn)密碼,我當(dāng)時(shí)公司就真有員工把這樣的密碼背下來了,現(xiàn)在想想真心是厲害~~~)。
##這樣看起來是比上面的好記一點(diǎn)了吧,但需要提供一個(gè)字典文件## $ 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 ...
代碼如下
#!/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 ##如果帶參數(shù)的話可以定義生成密碼的次數(shù),長(zhǎng)度,追溯記錄## 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
##字典文件(可以是各種單詞的組合)## $ cat word.txt chenyi itchenyi python ipython coreos coreos.me ipython.me
#!/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 ##如果帶參數(shù)的話可以定義生成密碼的次數(shù),長(zhǎng)度,追溯記錄## 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
##字典文件(可以是各種單詞的組合)## $ cat word.txt chenyi itchenyi python ipython coreos coreos.me ipython.me
相關(guān)文章
python端口掃描系統(tǒng)實(shí)現(xiàn)方法
這篇文章主要介紹了python端口掃描系統(tǒng)實(shí)現(xiàn)方法,可實(shí)現(xiàn)簡(jiǎn)單的外網(wǎng)IP掃描及寫入MySQL數(shù)據(jù)庫(kù)等功能,需要的朋友可以參考下2014-11-11一步步教你用python給女朋友寫個(gè)微信自動(dòng)提醒的程序
如今微信已成為我們?nèi)粘I畹闹饕涣鞴ぞ?但是微信自身的功能有時(shí)候可能并不能滿足我們的需要,因此我們會(huì)想是否可以進(jìn)行微信功能的拓展呢,這篇文章主要給大家介紹了關(guān)于利用python給女朋友寫了個(gè)微信自動(dòng)提醒程序的相關(guān)資料,需要的朋友可以參考下2021-10-10Python實(shí)現(xiàn)計(jì)算長(zhǎng)方形面積(帶參數(shù)函數(shù)demo)
今天小編就為大家分享一篇Python實(shí)現(xiàn)計(jì)算長(zhǎng)方形面積(帶參數(shù)函數(shù)demo),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01對(duì)Xpath 獲取子標(biāo)簽下所有文本的方法詳解
今天小編就為大家分享一篇對(duì)Xpath 獲取子標(biāo)簽下所有文本的方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01matlab中imadjust函數(shù)的作用及應(yīng)用舉例
這篇文章主要介紹了matlab中imadjust函數(shù)的作用及應(yīng)用舉例,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02