Python编程生成随机用户名及密码的方法示例
作者:难免有错_ 时间:2021-12-14 13:12:17
本文实例讲述了Python编程生成随机用户名及密码的方法。分享给大家供大家参考,具体如下:
方案一:
import random
global userName,userPassword #为了便于使用,定义为全局变量
userName = ''
userPassword = ''
def get_userNameAndPassword():
global userName, userPassword
usableName_char = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+=-><:}{?/" #可作为用户名的字符
usablePassword_char ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_.1234567890" #可作为密码的字符,根据所需可适当增减
e_userName = [] #定义一个临时List变量,使用list.append添加字符
e_userPassword = []
for i in range(8):
e_userName.append(random.choice(usableName_char))
for j in range(6):
e_userPassword.append(random.choice(usablePassword_char))
print"e_userName = ", e_userName #输出用户名字符list
print"e_userPassword = ", e_userPassword #输出密码字符list
userName = ''.join(e_userName)
userPassword = ''.join(e_userPassword)
try:
get_userNameAndPassword()
print "用户名:", userName
print "密码:", userPassword
except Exception, e:
print e.reason
程序输出:
e_userName = ['q', 'M', '2', 'R', 'B', '}', '6', '=']
e_userPassword = ['T', 'O', '4', 'C', 'H', '.']
用户名: qM2RB}6=
密码: TO4CH.
方案二(省去中间变量):
#coding=utf-8
import random
global userName,userPassword #为了便于后面使用,定义为全局变量
userName = ''
userPassword = ''
def get_userNameAndPassword():
global userName, userPassword
#8位用户名及6位密码
userName = ''.join(random.sample("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+=-><:}{?/",8))
userPassword = ''.join(random.sample("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_.1234567890",6))
try:
get_userNameAndPassword()
print "用户名:", userName
print "密码:", userPassword
except Exception, e:
print e.reason
程序输出:
用户名: GweV?2um
密码: fwiOZL
常用第二种方法,直观简便。
注:(本例在python2.7下测试正常运行。)
PS:这里再为大家提供两款相关在线工具供大家参考使用:
在线随机数字/字符串生成工具:
http://tools.jb51.net/aideddesign/suijishu
高强度密码生成器:
http://tools.jb51.net/password/CreateStrongPassword
希望本文所述对大家Python程序设计有所帮助。
标签:Python,随机,密码
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
发一段鼠标掠过表格行颜色改变的代码
2010-07-02 12:59:00
MySQL查询优化
2009-03-09 14:41:00
aspjpeg组件asp代码实例使用详解
2008-12-14 10:33:00
轻松掌握 MySQL的数字类型以及建库策略
2008-11-27 16:09:00
jQuery 让人恋恋不舍的秘密
2010-01-20 10:09:00
python模拟登录百度贴吧(百度贴吧登录)实例
2023-11-20 14:30:56
![](https://img.aspxhome.com/file/2023/4/63614_0s.jpg)
群组功能和用户沟通
2009-07-19 14:07:00
用Python实现web端用户登录和注册功能的教程
2021-03-03 07:49:09
![](https://img.aspxhome.com/file/2023/3/68323_0s.jpg)
MySQL内建复制功能来优化可用性
2010-10-25 20:20:00
Golang数据类型比较详解
2023-07-17 10:11:21
![](https://img.aspxhome.com/file/2023/5/93665_0s.jpg)
php将12小时制转换成24小时制的方法
2023-11-21 15:56:08
在CentOS 6 中安装WordPress(一) 安装Apache,Mysql, PHP环境
2023-11-06 17:41:24
![](https://img.aspxhome.com/file/2023/2/64962_0s.jpg)
centos yum php 7.x 无需删除升级的方法
2023-11-20 14:48:08
![](https://img.aspxhome.com/file/2023/5/79385_0s.jpg)
解析:以mysql_开始的未定义引用错误
2008-12-19 17:58:00
python类属性学习深入讲解
2021-03-25 06:05:34
![](https://img.aspxhome.com/file/2023/7/66747_0s.png)
Python一些基本的图像操作和处理总结
2022-02-24 05:46:33
![](https://img.aspxhome.com/file/2023/2/65562_0s.png)
Oracle 10g的DBA无法登录解决方案
2009-05-24 19:38:00
antd项目实现彩蛋效果的详细代码
2023-09-14 12:51:20
![](https://img.aspxhome.com/file/2023/1/56321_0s.jpg)
数字人组件反写[asp组件开发实例4]
2009-06-09 13:20:00
Python模拟键盘输入自动登录TGP
2021-11-04 11:27:22