赞
踩
import random
2import string
3
4
5total = string.ascii_letters + string.digits + string.punctuation
6
7
8length = 8
9
10
11password = "".join(random.sample(total, length))
12
13
14print(password)
import random 2import math 3 4 5alpha = "abcdefghijklmnopqrstuvwxyz" 6num = "0123456789" 7special = "@#$%&*" 8 9 10# 输入密码长度 11pass_len = int(input("输入密码长度")) 12 13 14# 设置密码长度组成格式,字母数字和特殊符号占比,以下示例50-30-20比例分配 15alpha_len = pass_len//2 16num_len = math.ceil(pass_len*30/100) 17special_len = pass_len-(alpha_len+num_len) 18 19 20password = [] 21 22 23def generate_pass(length, array, is_alpha=False): 24 for i in range(length): 25 index = random.randint(0, len(array) - 1) 26 character = array[index] 27 if is_alpha: 28 case = random.randint(0, 1) 29 if case == 1: 30 character = character.upper() 31 password.append(character) 32 33 34# 字符 35generate_pass(alpha_len, alpha, True) 36# 数字 37generate_pass(num_len, num) 38# 特殊符号 39generate_pass(special_len, special) 40# 打乱顺序 41random.shuffle(password) 42# 转列表到字符串 43gen_password = "" 44for i in password: 45 gen_password = gen_password + str(i) 46print(gen_password)
python常用的demo集合,待后续。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。