python打印随机密码

  开发

# coding:utf-8
import random



def gen(pwd_len):
    uperChar = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    lowerChar = 'abcdefghijklmnopqrstuvwxyz'
    num = '1234567890'
    specChar = '_'
    ###gen
    n_uperChar = random.randint(0, len(uperChar) - 1)
    n_lowerChar = random.randint(0, len(lowerChar) - 1)
    n_num = random.randint(0, len(num) - 1)
    n_specChar = random.randint(0, len(specChar) - 1)
    password_base = uperChar[n_uperChar] + lowerChar[n_lowerChar] + num[n_num] + specChar[n_specChar]
    password_set = uperChar + lowerChar + num + specChar
    password_set_list = list(password_set)
    random.shuffle(password_set_list)
    while len(password_set_list) < pwd_len:
        # MemoryError
        password_set_list = password_set_list + password_set_list
    password_list = password_set_list[0:pwd_len - 4] + list(password_base)
    random.shuffle(password_list)
    return ''.join(password_list)

if __name__ == '__main__':
    print(gen(15))

LEAVE A COMMENT

Captcha Code