python如何生成随机密码?


当前第2页 返回上一页

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

import random

random.seed(0x1010)  #设置随机种子数

    #设置种子选择空间

s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*"

ls = [] #存取密码的列表

FirstPsw = "" #存取第一个密码的字符

  

while len(ls)<20:  #十个随机密码

    pwd = ""

    for i in range(10):

        pwd += s[random.randint(0,len(s)-1)]

    if pwd[0] in FirstPsw:

        continue

    else:

        ls.append(pwd)

        FirstPsw +=pwd[0]

fo = open("随机密码.txt","w",encoding ="utf-8")

fo.write("\n".join(ls))

fo.close()

三、Python生成8位必含数字、大小写字母的字符串(密码)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

#-*-coding:utf_8-*-

import random,string    #调用random、string模块

src_digits = string.digits              #string_数字

src_uppercase = string.ascii_uppercase  #string_大写字母

src_lowercase = string.ascii_lowercase  #string_小写字母

count = int(input("请输入生成密码个数:"))

for i in range(count):

    #随机生成数字、大写字母、小写字母的组成个数(可根据实际需要进行更改)

    digits_num = random.randint(1,6)

    uppercase_num = random.randint(1,8-digits_num-1)

    lowercase_num = 8 - (digits_num + uppercase_num)

    #生成字符串

    password = random.sample(src_digits,digits_num) + random.sample(src_uppercase,uppercase_num) + random.sample(src_lowercase,lowercase_num)

    #打乱字符串

    random.shuffle(password)

    #列表转字符串

    new_password = ''.join(password)

    print(new_password)

推荐教程:《python视频教程》

以上就是python如何生成随机密码?的详细内容,更多文章请关注木庄网络博客!!

返回前面的内容

相关阅读 >>

Python中子类如何调用父类函数的代码示例

Python和selenium以及autoit如何实现文件上传功能的图文代码详解

Python实现微信推送模板消息功能示例

Python 装饰器

Python实现简单http服务器

Python处理excel xlrd的方法介绍

Python取余运算符是什么

Python统计序列中元素的方法

Python操作mysql代码总结

Python实现逐行分割大txt文件的方法介绍

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...