随机生成密码

#1、必须包含大小写字母、数字、特殊字符,密码不能以特殊字符开头,长度在8-15


#1、先产生一个随机的长度,8
#2、先从所有的大写字母里面取2位,再从所有的小写字母取2位,再从数字里面取2,再从特殊字符串里面取2个

#1、先产生一个随机的长度,8,然后从大小写字母、数字、特殊字符,取几位
#2、分别和


import random
import string

def create_password(num):
passwords = set()
while len(passwords) != num:
length = random.randint(8,15)
password1 = random.sample(string.ascii_letters+string.digits+string.punctuation,length)
if set(password1) & set(string.ascii_uppercase) and set(password1) & set(string.ascii_lowercase) and
set(password1) & set(string.digits) and set(password1) & set(string.punctuation) and password1[0] not in string.punctuation:
password = ''.join(password1) +' '
passwords.add(password)
with open('passwords.txt','w') as fw:
fw.writelines(passwords)
原文地址:https://www.cnblogs.com/Dorami/p/11039637.html