有严规则的密码生成器
import random from random import shuffle import string def generatepassword(): key='' for i in range(requirement['lowercase']): key=key+random.choice(lowercase) for i in range(requirement['uppercase']): key=key+random.choice(uppercase) for i in range(requirement['digits']): key=key+random.choice(digits) for i in range(requirement['special']): key=key+random.choice(special) for i in range(requirement['length']-requirement['lowercase']-requirement['uppercase']-requirement['digits']-requirement['special']): key=key+random.choice(fulltable) key=list(key) generatekey=random.shuffle(key) return ''.join(key) if __name__ == "__main__": print("默认的密码是16位,有2位大写,2位小写,2个特殊字符,2个数字 ") lowercase='abcdefghijklmnopqrstuvwxyz' uppercase='ABCDEFGHIJKLMNOPQRSTUVWXYZ' digits='0123456789' special= """!"#$%&'()*+,-./:;<=>?@[]^_`{|}~""" fulltable = lowercase+uppercase+digits+special requirement = {'lowercase' : 2 ,'uppercase': 2,'digits' :2,'special' : 2,'length':16} for j in range(10): print(generatepassword())
结果如下
默认的密码是16位,有2位大写,2位小写,2个特殊字符,2个数字
X:6u4G.k6%70*Gce
y^+P&4Q)>a=Rl'0N
c_R2@)eD?m74DP`-
QDOd0h#j#B{qlS79
PtN)9N^T7~J}oJaZ
$Q|6}70j~p<A~Roj
Ch;]T-2xK"8ei{10
0)y3~VsDDj={3/F!
>4{P'C2si,{8@6=K
w-G-(3Yt61Zwu!^I
Process finished with exit code 0