python31实例[生成随即的密码]
时间:2011-01-19 来源:iTech
代码:
import random
import string
import time
def mkpass(size=8):
chars = []
chars.extend([i for i in string.ascii_letters])
chars.extend([i for i in string.digits])
chars.extend([i for i in '\'"!@#$%&*()-_=+[{}]~^,<.>;:/?'])
passwd = ''
for i in range(size):
passwd += chars[random.randint(0, len(chars) - 1)]
random.seed = int(time.time())
random.shuffle(chars)
return passwd
print( mkpass())
def rule1(ch):
rulesdict = { 'o': 0, 'i':'!', 'b':8, 'p':'P', 'm':'M'}
newch = ch
if ch in rulesdict.keys():
newch = rulesdict[ch]
return newch
def mkpassbyrules(passwd, rule):
newpasswd = ""
for c in passwd:
ch = rule(c)
newpasswd = newpasswd + str(ch)
return newpasswd
print(mkpassbyrules('password', rule1))
print(mkpassbyrules('mygod123', rule1))
参考:http://code.activestate.com/recipes/577339-random-passwords/
完!
相关阅读 更多 +