# 生成指定长度的字母数字密码 defletter_digit_passwd(length): # s='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' s = string.ascii_letters + string.digits passwd = ''.join(secrets.choice(s) for i inrange(length)) return passwd
# 生成指定长度的字母数字密码:至少1个小写字母,upper_num个大写字母和digit_num个数字 defletter_digit_passwd2(length, upper_num, digit_num): s = string.ascii_letters + string.digits whileTrue: password = ''.join(secrets.choice(s) for i inrange(length)) if (any(c.islower() for c in password) andsum(c.isupper() for c in password) >= upper_num andsum(c.isdigit() for c in password) >= digit_num): break return password