使用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实现猜数字游戏(无重复数字)示例分享
Mar 29 Python
Python易忽视知识点小结
May 25 Python
Python实现二分查找与bisect模块详解
Jan 13 Python
Python使用SQLite和Excel操作进行数据分析
Jan 20 Python
Python编写一个优美的下载器
Apr 15 Python
python 获取utc时间转化为本地时间的方法
Dec 31 Python
Python中按键来获取指定的值
Mar 02 Python
python的依赖管理的实现
May 14 Python
Python编写打字训练小程序
Sep 26 Python
python中自带的三个装饰器的实现
Nov 08 Python
解决Keras中Embedding层masking与Concatenate层不可调和的问题
Jun 18 Python
python使用bs4爬取boss直聘静态页面
Oct 10 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知道与问问的采集插件代码
2010/10/12 PHP
PHP中ob_start函数的使用说明
2013/11/11 PHP
zen_cart实现支付前生成订单的方法
2016/05/06 PHP
PHP单例模式与工厂模式详解
2017/08/29 PHP
PHP实现QQ登录的开原理和实现过程
2018/02/04 PHP
php中get_object_vars()在数组的实例用法
2021/02/22 PHP
用JQUERY增删元素的代码
2012/02/14 Javascript
ajax与302响应代码测试
2013/10/23 Javascript
jquery 操作iframe的几种方法总结
2013/12/13 Javascript
简介JavaScript中substring()方法的使用
2015/06/06 Javascript
JavaScript 监控微信浏览器且自带返回按钮时间
2016/11/27 Javascript
js实现把时间戳转换为yyyy-MM-dd hh:mm 格式(es6语法)
2017/12/28 Javascript
Vue循环组件加validate多表单验证的实例
2018/09/18 Javascript
vue  directive定义全局和局部指令及指令简写
2018/11/20 Javascript
微信小程序实现左右列表联动
2020/05/19 Javascript
JavaScript switch语句使用方法简介
2019/12/30 Javascript
Vue使用CDN引用项目组件,减少项目体积的步骤
2020/10/30 Javascript
[07:55]2014DOTA2 TI正赛第三日 VG上演推进荣耀DKEG告别
2014/07/21 DOTA
Python实现去除列表中重复元素的方法小结【4种方法】
2018/04/27 Python
用vue.js组件模拟v-model指令实例方法
2019/07/05 Python
python:动态路由的Flask程序代码
2019/11/22 Python
用什么库写 Python 命令行程序(示例代码详解)
2020/02/20 Python
Python MySQL 日期时间格式化作为参数的操作
2020/03/02 Python
python能做哪方面的工作
2020/06/15 Python
python转化excel数字日期为标准日期操作
2020/07/14 Python
JupyterNotebook 输出窗口的显示效果调整实现
2020/09/22 Python
使用python把xmind转换成excel测试用例的实现代码
2020/10/12 Python
详解python中的三种命令行模块(sys.argv,argparse,click)
2020/12/15 Python
几款主流好用的富文本编辑器(所见即所得常用编辑器)介绍
2021/03/17 Javascript
CSS实现聊天气泡效果
2020/04/26 HTML / CSS
美国领先的在线旅游网站:Orbitz
2018/11/05 全球购物
卫校中专生的自我评价
2014/01/15 职场文书
2014年中秋寄语
2014/08/11 职场文书
2015年图书馆个人工作总结
2015/05/26 职场文书
Python 中random 库的详细使用
2021/06/03 Python
python垃圾回收机制原理分析
2022/04/13 Python