使用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使用socket远程连接错误处理方法
Apr 29 Python
正确理解python中的关键字“with”与上下文管理器
Apr 21 Python
基于Python pip用国内镜像下载的方法
Jun 12 Python
python列表,字典,元组简单用法示例
Jul 11 Python
Django使用 Bootstrap 样式修改书籍列表过程解析
Aug 09 Python
Python csv模块使用方法代码实例
Aug 29 Python
python3实现绘制二维点图
Dec 04 Python
深入浅析Python代码规范性检测
Jul 31 Python
Selenium 配置启动项参数的方法
Dec 04 Python
使用sublime text3搭建Python编辑环境的实现
Jan 12 Python
python使用scapy模块实现ARP扫描的过程
Jan 21 Python
使用Python爬取小姐姐图片(beautifulsoup法)
Feb 11 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
《超神学院》霸气归来, 天使彦上演维多利亚的秘密
2020/03/02 国漫
在php中取得image按钮传递的name值
2006/10/09 PHP
深入理解PHP原理之错误抑制与内嵌HTML分析
2011/05/02 PHP
ThinkPHP自动验证失败的解决方法
2011/06/09 PHP
fgetcvs在linux的问题
2012/01/15 PHP
PHP版单点登陆实现方案的实例
2016/11/17 PHP
PHP封装的完整分页类示例
2018/08/21 PHP
PHP实现基于3DES算法加密解密字符串示例
2018/08/24 PHP
php的无刷新操作实现方法分析
2020/02/28 PHP
Javascript 面向对象 重载
2010/05/13 Javascript
firefox事件处理之自动查找event的函数(用于onclick=foo())
2010/08/05 Javascript
jquery对table中各数据的增加、保存、删除操作示例
2014/05/14 Javascript
jquery实现两个图片渐变切换效果的方法
2015/06/25 Javascript
JS拖动鼠标画出方框实现鼠标选区的方法
2015/08/05 Javascript
jQuery中的通配符选择器使用总结
2016/05/30 Javascript
打造自己的jQuery插件入门教程
2016/09/23 Javascript
基于wordpress的ajax写法详解
2018/01/02 Javascript
VUE 使用中踩过的坑
2018/02/08 Javascript
微信小程序上传图片功能(附后端代码)
2020/06/19 Javascript
微信小程序拼接图片链接无底洞深入探究
2019/09/03 Javascript
js基于canvas实现时钟组件
2021/02/07 Javascript
[47:03]完美世界DOTA2联赛PWL S3 Galaxy Racer vs Phoenix 第二场 12.10
2020/12/13 DOTA
Python中GeoJson和bokeh-1的使用讲解
2019/01/03 Python
Python占用的内存优化教程
2019/07/28 Python
使用Keras实现简单线性回归模型操作
2020/06/12 Python
python如何删除列为空的行
2020/07/17 Python
CSS3对图片照片进行边缘模糊处理的实现
2018/08/08 HTML / CSS
移动端Html5页面生成图片解决方案
2018/08/07 HTML / CSS
法国购买隐形眼镜和眼镜网站:Optical Center
2019/10/08 全球购物
销售团队口号大全
2014/06/06 职场文书
体育节口号
2014/06/19 职场文书
倡议书的写法
2014/08/30 职场文书
服务行业演讲稿
2014/09/02 职场文书
入党自荐书范文
2015/03/05 职场文书
放假通知格式
2015/04/14 职场文书
GO语言字符串处理函数之处理Strings包
2022/04/14 Golang