Python随机生成8位密码的示例详解
作者:吃肉的小馒头 时间:2023-09-15 21:04:03
代码示例1(8位)
import random
import string
total = string.ascii_letters + string.digits + string.punctuation
length = 8
password = "".join(random.sample(total, length))
print(password)
代码示例2(任意)
import random
import math
alpha = "abcdefghijklmnopqrstuvwxyz"
num = "0123456789"
special = "@#$%&*"
# 输入密码长度
pass_len = int(input("输入密码长度"))
# 设置密码长度组成格式,字母数字和特殊符号占比,以下示例50-30-20比例分配
alpha_len = pass_len//2
num_len = math.ceil(pass_len*30/100)
special_len = pass_len-(alpha_len+num_len)
password = []
def generate_pass(length, array, is_alpha=False):
for i in range(length):
index = random.randint(0, len(array) - 1)
character = array[index]
if is_alpha:
case = random.randint(0, 1)
if case == 1:
character = character.upper()
password.append(character)
# 字符
generate_pass(alpha_len, alpha, True)
# 数字
generate_pass(num_len, num)
# 特殊符号
generate_pass(special_len, special)
# 打乱顺序
random.shuffle(password)
# 转列表到字符串
gen_password = ""
for i in password:
gen_password = gen_password + str(i)
print(gen_password)
知识补充
除了上面的两个示例代码,小编还为大家整理了一些Python实现生成随机密码的方法,希望对大家有所帮助
python实现生成随机 字母 + 数字密码
#coding:utf-8
import random,string
def GetPassword(length):
# 随机生成数字个数
Ofnum=random.randint(1,length)
Ofletter=length-Ofnum
# 选中ofnum个数字
slcNum=[random.choice(string.digits) for i in range(Ofnum)]
# 选中ofletter个字母
slcLetter=[random.choice(string.ascii_letters) for i in range(Ofletter)]
# 打乱组合
slcChar=slcLetter+slcNum
random.shuffle(slcChar)
# 生成随机密码
getPwd=''.join([i for i in slcChar])
return getPwd
if __name__=='__main__':
print( GetPassword(6)) #GetPassword()自定义随机密码长度
Python随机生成带特殊字符的密码
#!/usr/bin/env python
import string
from itertools import chain
from random import choice, sample
def mkpasswd(length=12, digits=4, upper=3, lower=3):
lowercase = string.lowercase
uppercase = string.uppercase
salt = '!@#$%^&*()><?'
password = list(
chain(
(choice(uppercase) for _ in range(upper)),
(choice(lowercase) for _ in range(lower)),
(choice(string.digits) for _ in range(digits)),
(choice(salt) for _ in range((length - digits - upper - lower)))
)
)
return "".join(sample(password, len(password)))
if __name__ == '__main__':
print mkpasswd()
#!/usr/bin/env python
import string
from itertools import chain
from random import choice, sample
def mkpasswd(length=12, digits=4, upper=3, lower=3):
lowercase = string.lowercase
uppercase = string.uppercase
salt = '!@#$%^&*()><?'
password = list(
chain(
(choice(uppercase) for _ in range(upper)),
(choice(lowercase) for _ in range(lower)),
(choice(string.digits) for _ in range(digits)),
(choice(salt) for _ in range((length - digits - upper - lower)))
)
)
return "".join(sample(password, len(password)))
if __name__ == '__main__':
print mkpasswd()
来源:https://blog.csdn.net/qq_42034590/article/details/128994108
标签:Python,随机,生成,密码
0
投稿
猜你喜欢
对python当中不在本路径的py文件的引用详解
2022-12-17 15:12:42
新手必备Python开发环境搭建教程
2023-07-09 03:25:35
python简单实现最大似然估计&scipy库的使用详解
2022-11-03 23:45:01
Python实现K-means聚类算法并可视化生成动图步骤详解
2021-06-20 23:10:40
python排序算法之归并排序
2021-03-24 06:05:39
Pandas中的 transform()结合 groupby()用法示例详解
2023-01-26 10:00:09
php strftime函数的详细用法
2023-06-07 19:09:37
Python 实现购物商城,含有用户入口和商家入口的示例
2023-09-26 11:39:46
JS完美实现对象克隆
2008-08-03 16:51:00
Vue路由切换和Axios接口取消重复请求详解
2024-04-30 10:28:17
数据库性能优化一:数据库自身优化提升性能
2024-01-24 04:02:07
JavaScript Base64编码和解码,实现URL参数传递。
2024-04-22 22:45:32
Python OpenCV中的resize()函数的使用
2022-03-23 23:11:56
python datetime处理时间小结
2022-11-21 01:38:15
CSS和HTML与前端技术层图示
2010-04-05 21:54:00
Django框架模板语言实例小结【变量,标签,过滤器,继承,html转义】
2023-11-11 06:41:39
关于SQL Server查询语句的使用
2024-01-16 08:14:06
js几个不错的函数 $$()
2024-04-29 13:41:11
怎么才能限制SQL Server只能让指定的机器连接
2010-07-30 13:30:00
Python调用Prometheus监控数据并计算
2023-12-01 02:18:39