使用Python生成随机密码的示例分享


Posted in Python onFebruary 18, 2016

生成随机密码这件事情用python来干确实相当的方便,优美的string方法加上choice简直是绝配
make_password.py

###简单几行代码执行即可生成记不住的字符串###

$ python make_passwd.py
 DLrw9EiT
 Qs4Wm84q
 RQwl4L2L
 u9g0LgwW
 jHPtYdyU
 ...
$ python make_passwd.py
 DLrw9EiT
 Qs4Wm84q
 RQwl4L2L
 u9g0LgwW
 jHPtYdyU
 ...

代码如下——注释比代码长

#!/usr/bin/python
 #--coding:utf-8--#
 #-------------------------------------------------------------------------------
 # Name: make_passwd
 #
 # Author: LiuSha
 #
 # Created: 28/12/2014
 # Copyright: (c) WDZJ-SA 2014
 #-------------------------------------------------------------------------------
 from random import choice
 import string
def Makepass(length=8, chars=string.letters+string.digits):
 return ''.join([choice(chars) for i in range(length)])
if __name__ == '__main__':
 for i in range(10):
 print Makepass()
##下例基本上就是这个小脚本的所有工作核心了,使用random模块的choice方法取string模块生成的字符串##
 >>> string.letters
 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
 >>> string.digits
 '0123456789'
>>> choice(string.digits)
 '4'
 >>> choice(string.letters)
 'T'
##有关生成器可参考:http://www.ipython.me/python/python-generator.html##

#!/usr/bin/python
 #--coding:utf-8--#
 #-------------------------------------------------------------------------------
 # Name: make_passwd
 #
 # Author: LiuSha
 #
 # Created: 28/12/2014
 # Copyright: (c) WDZJ-SA 2014
 #-------------------------------------------------------------------------------
 from random import choice
 import string
def Makepass(length=8, chars=string.letters+string.digits):
 return ''.join([choice(chars) for i in range(length)])
if __name__ == '__main__':
 for i in range(10):
 print Makepass()
##下例基本上就是这个小脚本的所有工作核心了,使用random模块的choice方法取string模块生成的字符串##
 >>> string.letters
 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
 >>> string.digits
 '0123456789'
>>> choice(string.digits)
 '4'
 >>> choice(string.letters)
 'T'
##有关生成器可参考:http://www.ipython.me/python/python-generator.html##

 
生成一些人似乎能好记一些的密码(Qs4Wm84q这种密码似乎除了复制粘贴没有别的选择,话说前年我使用shell生成的类似的密码给ldap做默认密码,我当时公司就真有员工把这样的密码背下来了,现在想想真心是厉害~~~)。

##这样看起来是比上面的好记一点了吧,但需要提供一个字典文件##

$ python make_dictpass.py 1 8 1
 ipythosd
$ python make_dictpass.py
 nahontchen
 chenyibfeo
 ipythoniue
 coreostche
 ...

$ python make_dictpass.py 1 8 1
 ipythosd
$ python make_dictpass.py
 nahontchen
 chenyibfeo
 ipythoniue
 coreostche
 ...

代码如下

#!/usr/bin/python
 #--coding:utf-8--#
 #-------------------------------------------------------------------------------
 # Name: make_dictpass
 #
 # Author: LiuSha
 #
 # Created: 28/12/2014
 # Copyright: (c) WDZJ-SA 2014
 #-------------------------------------------------------------------------------
 import random
 import string
class passwd():
 data = open('./word.txt').read().lower()
 def renew(self, n, maxmem=3):
 self.chars = []
 for i in range(n):
 randspot = random.randrange(len(self.data))
 self.data = self.data[randspot:] + self.data[:randspot]
 where = -1
 locate = ''.join(self.chars[-maxmem:])
 while where < 0 and locate: where = self.data.find(locate) locate = locate[1:] c = self.data[where+len(locate)+1] if not c.islower(): c = random.choice(string.lowercase) self.chars.append(c) def __str__(self): return ''.join(self.chars) if __name__ == '__main__': import sys ##如果带参数的话可以定义生成密码的次数,长度,追溯记录## if len(sys.argv) > 1:
 dopass = int(sys.argv[1])
 else:
 dopass = 8
 if len(sys.argv) > 2:
 length = int(sys.argv[2])
 else:
 length = 10
 if len(sys.argv) > 3:
 memory = int(sys.argv[3])
 else:
 memory = 3
 onepass = passwd()
 for i in range(dopass):
 onepass.renew(length,memory)
 print onepass
##字典文件(可以是各种单词的组合)##
 $ cat word.txt
 chenyi
 itchenyi
 python
 ipython
 coreos
 coreos.me
 ipython.me
#!/usr/bin/python
 #--coding:utf-8--#
 #-------------------------------------------------------------------------------
 # Name: make_dictpass
 #
 # Author: LiuSha
 #
 # Created: 28/12/2014
 # Copyright: (c) WDZJ-SA 2014
 #-------------------------------------------------------------------------------
 import random
 import string
class passwd():
 data = open('./word.txt').read().lower()
 def renew(self, n, maxmem=3):
 self.chars = []
 for i in range(n):
 randspot = random.randrange(len(self.data))
 self.data = self.data[randspot:] + self.data[:randspot]
 where = -1
 locate = ''.join(self.chars[-maxmem:])
 while where < 0 and locate: where = self.data.find(locate) locate = locate[1:] c = self.data[where+len(locate)+1] if not c.islower(): c = random.choice(string.lowercase) self.chars.append(c) def __str__(self): return ''.join(self.chars) if __name__ == '__main__': import sys ##如果带参数的话可以定义生成密码的次数,长度,追溯记录## if len(sys.argv) > 1:
 dopass = int(sys.argv[1])
 else:
 dopass = 8
 if len(sys.argv) > 2:
 length = int(sys.argv[2])
 else:
 length = 10
 if len(sys.argv) > 3:
 memory = int(sys.argv[3])
 else:
 memory = 3
 onepass = passwd()
 for i in range(dopass):
 onepass.renew(length,memory)
 print onepass
##字典文件(可以是各种单词的组合)##
 $ cat word.txt
 chenyi
 itchenyi
 python
 ipython
 coreos
 coreos.me
 ipython.me
Python 相关文章推荐
Python中使用glob和rmtree删除目录子目录及所有文件的例子
Nov 21 Python
Python数组遍历的简单实现方法小结
Apr 27 Python
Python基于回溯法子集树模板实现图的遍历功能示例
Sep 05 Python
pandas 实现字典转换成DataFrame的方法
Jul 04 Python
对python多线程与global变量详解
Nov 09 Python
深入了解Python iter() 方法的用法
Jul 11 Python
简单了解django orm中介模型
Jul 30 Python
python中几种自动微分库解析
Aug 29 Python
使用 Python 清理收藏夹里已失效的网站
Dec 03 Python
python进行参数传递的方法
May 12 Python
python将数据插入数据库的代码分享
Aug 16 Python
jupyter notebook更换皮肤主题的实现
Jan 07 Python
使用Python的urllib2模块处理url和图片的技巧两则
Feb 18 #Python
讲解Python的Scrapy爬虫框架使用代理进行采集的方法
Feb 18 #Python
使用Python的PIL模块来进行图片对比
Feb 18 #Python
使用Python来编写HTTP服务器的超级指南
Feb 18 #Python
python装饰器与递归算法详解
Feb 18 #Python
Python利用Nagios增加微信报警通知的功能
Feb 18 #Python
Python多线程、异步+多进程爬虫实现代码
Feb 17 #Python
You might like
PHP 组件化编程技巧
2009/06/06 PHP
PHP5 面向对象(学习记录)
2009/12/02 PHP
php 数组使用详解 推荐
2011/06/02 PHP
php学习笔记之 函数声明
2011/06/09 PHP
mongo Table类文件 获取MongoCursor(游标)的实现方法分析
2013/07/01 PHP
PHP 下载文件时自动添加bom头的方法实例
2014/01/10 PHP
PHP按行读取、处理较大CSV文件的代码实例
2014/04/09 PHP
jQuery实现 注册时选择阅读条款 左右移动
2013/04/11 Javascript
js图片自动切换效果处理代码
2013/05/07 Javascript
JQuery获取样式中的background-color颜色值的问题
2013/08/20 Javascript
Javascript封装DOMContentLoaded事件实例
2014/06/12 Javascript
javascript中数组array及string的方法总结
2014/11/28 Javascript
通过设置CSS中的position属性来固定层的位置
2015/12/14 Javascript
JavaScript绑定事件监听函数的通用方法
2016/05/14 Javascript
拖动时防止选中
2017/02/03 Javascript
JS实现css hover操作的方法示例
2017/04/07 Javascript
JS中‘hello’与new String(‘hello’)引出的问题详解
2018/08/14 Javascript
js计算两个日期间的天数月的实例代码
2018/09/20 Javascript
bootstrap实现嵌套模态框的实例代码
2020/01/10 Javascript
Element-ui el-tree新增和删除节点后如何刷新tree的实例
2020/08/31 Javascript
用python登录Dr.com思路以及代码分享
2014/06/25 Python
使用 Python 获取 Linux 系统信息的代码
2014/07/13 Python
Python编程二分法实现冒泡算法+快速排序代码示例
2018/01/15 Python
python 脚本生成随机 字母 + 数字密码功能
2018/05/26 Python
python3+selenium实现qq邮箱登陆并发送邮件功能
2019/01/23 Python
python列表每个元素同增同减和列表元素去空格的实例
2019/07/20 Python
Python基于xlrd模块处理合并单元格
2020/07/28 Python
Python爬虫与反爬虫大战
2020/07/30 Python
HTML5 3D衣服摇摆动画特效
2016/03/17 HTML / CSS
领先的荷兰线上超市:荷兰之家Holland at Home(支持中文)
2021/01/21 全球购物
资深财务管理人员自我评价
2013/09/22 职场文书
高二地理教学反思
2014/01/24 职场文书
学校创先争优活动总结
2014/08/28 职场文书
党建工作整改措施
2014/10/28 职场文书
人事聘任通知
2015/04/21 职场文书
《巫师》是美食游戏?CDPR10月将推出《巫师》官方食谱
2022/04/03 其他游戏