import random def have_number(password_str): numbers = {'0','1','2','3','4','5','6','7','8','9'} return set(password_str) & numbers def have_letter(password_str): letters = set('abcdefghijklmnopqrstuvwxyz') return letters & set(password_str.lower()) def makenumberstr(): """随机产生一个3到10位的数字字符串""" n = '0123456789' return "".join( [random.choice(n) for _ in range(random.randint(3,10))]) def makalphastr(): """随机产生一个3到10位的字母字符串""" s = 'abcdefghijklmnopqrstuvwxyz' return "".join( [random.choice(s) for _ in range(random.randint(3,10))]) def makerandomstr(): """随机产生一个3到10位的包含数字和字母的字符串""" s = '0123456789abcdefghijklmnopqrstuvwxyz' return "".join( [random.choice(s) for _ in range(random.randint(3,10))]) def makepass(level): """根据密码强度产生密码""" if level == 1: p = makerandomstr() while len(p)>=8:p = makerandomstr() elif level == 2: # 如果强度是2,则产生大于或等于8位的仅是数字或字母的密码 if random.randint(0,1)==0: # 如果是0,产生大于或等于8位的数字密码 p = makenumberstr() while len(p)<8:p = makenumberstr() else: p = makalphastr() while len(p)<8:p = makalphastr() elif level == 3: # 如果强度是3,则产生大于8位的包含数字和字母的密码 p = makerandomstr() while len(p)<8 or not have_number(p) or not have_letter(p): p = makerandomstr() return p for _ in range(10): level = random.randint(1,3) print(level,makepass(level))
李兴球
李兴球的博客是Python创意编程原创博客
要发表评论,您必须先登录。
发表评论