Python一行生成随机密码

Python一行生成16位随机密码  https://www.cnblogs.com/iAmSoScArEd/p/14188138.html---我超怕的

 

import random
random_pwd="".join(random.sample('abcdefghijklmnopqrstuvwxyz!@#$%+=-_^&*.?ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890',16))
print(random_pwd)

 

生成指定位数、指定格式的随机密码 

示例:

字符+数字+大写字母+小写字母(2-3位字符,4-5位数字,4-5位大写字母,3-6小写字母)

import random
  
def get_random_password():
  # 16位的密码 total = 16 # 2-3位字符,4-5位数字,4-5位大写字母,3-6小写字母 num = random.randint(2,3) num1 = random.randint(4,5) num2 = random.randint(4,5) num3 = random.randint(total-num-num1-num2,total-num-num1-num2) password = random.sample('!~@_=?#$%^&*()-+.',num) password1 = random.sample('0123456789',num1) password2 = random.sample('ABCDEFGHIJKLMNOPQRSTUVWXYZ',num2) password3 = random.sample('abcdefghijklmnopqrstuvwxyz',num3) #print('{}:{}\n{}:{}\n{}:{}\n{}:{}'.format(num,password,num1,password1,num2,password2,num3,password3)) newPasswordList = password + password1 + password2 + password3 random.shuffle(newPasswordList) newPassword = ''.join(newPasswordList) # print(newPassword) return newPassword

 

posted @ 2020-12-25 11:41  我超怕的  阅读(460)  评论(2编辑  收藏  举报