产生密码

 1 '''
 2 写一个产生密码的程序,输入几就产生多少条密码,存到passwords.txt里面
 3            1、密码要求,长度在8-15之间随机
 4            2、密码里面必须包括数字、大写字母、小写字母
 5            3、产生的同一批里面不能有重复的
 6            '''
 7 
 8 import random,string
 9 
10 def create_password(num,filename="passwords.txt"): #10
11     all_password = set()
12     while num !=len(all_password):
13         length = random.randint(8, 15)
14         s2 = random.sample(string.digits+string.ascii_letters,length)
15         if set(s2) & set(string.ascii_uppercase) and set(s2) & set(string.ascii_lowercase) and set(s2) & \
16             set(string.digits):
17             password = ''.join(s2) + '\n'
18             all_password.add(password)
19 
20     with open(filename,'w') as f:
21         f.writelines(all_password)
22     print("密码生成完成")
23 
24 
25 def create_password2(num,filename="passwords.txt"):  # 10
26     all_password = set()
27     while num != len(all_password):
28         length = random.randint(8, 15)
29         s1 = random.choice(string.ascii_lowercase)  + random.choice(string.ascii_uppercase) \
30         + random.choice(string.digits)
31         s2 = random.sample(string.ascii_letters+string.digits,length-3)
32         s2.append(s1) #['s','c','5','A','2','sfd']
33         random.shuffle(s2)
34         password = ''.join(s2) + '\n'
35         all_password.add(password)
36 
37     with open(filename, 'w') as f:
38         f.writelines(all_password)
39     print("密码生成完成")
40 
41 
42 if __name__ == '__main__':
43     create_password(100)
44     create_password2(100,'password2.txt')

 

posted @ 2021-07-02 16:00  等待iing  阅读(97)  评论(0)    收藏  举报