何以使Python生成随机密码

形容了个程序,主要是故来检测MySQL数据库的空密码和弱密码的,

于此,定义了三类似弱密码:

  1. 接连数字,譬如123456,在get_weak_num中实现

  2. 连续字母,譬如abcdef,在get_weak_character中实现

本,个数都是即兴的。

  1. 数字与字母随机做。在get_weak_num_character中实现。

与此同时定义了一个password_exist的列表,用于保存不同之密码。如果新变化的密码在列表中设有,则免开展MySQL数据库的接连,直接到下同样蹩脚巡回。

现实如下:

#coding=utf8
import random,string,MySQLdb
def get_num():
    return random.randint(0,9)
def get_char():
    return random.choice(tuple(string.lowercase))
def choose_any():
    return [str(get_num()),get_char()]
def get_weak_num():
    weak_num=[]
    initial_num=get_num()
    for i in range(get_num()):
        weak_num.append(str(initial_num+i))
        if initial_num +i ==9:
            break;
    return weak_num
def get_weak_character():
    weak_character=[]
    initial_character=get_char()
    for i in range(get_num()):
        weak_character.append(chr(ord(initial_character)+i))
        if chr(ord(initial_character)+i) == 'z':
            break
    return weak_character
def get_weak_num_character():
    return [random.choice(choose_any()) for num in range(get_num())]
password_exist=[]
for i in range(10000):
    choice = [get_weak_num(), get_weak_character(), get_weak_num_character()]
    password=''.join(random.choice(choice))
    print "第"+str(i)+"次密码为:"+password
    if password in password_exist:
        continue
    else:
        try:
            MySQLdb.connect('192.168.244.145', 'root', password)
            print 'The password for MySQL is:'+password
            break
        except:
            continue
        password_exist.append(password)
if i == 9999:
    print 'The password is not so weak~'

 

网站地图xml地图