使用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写xml文件的操作实例
Oct 05 Python
Python合并多个装饰器小技巧
Apr 28 Python
python实现文本去重且不打乱原本顺序
Jan 26 Python
django项目中使用手机号登录的实例代码
Aug 15 Python
Django实现简单网页弹出警告代码
Nov 15 Python
TensorFlow——Checkpoint为模型添加检查点的实例
Jan 21 Python
Python多线程Threading、子线程与守护线程实例详解
Mar 24 Python
tensorflow模型转ncnn的操作方式
May 25 Python
部署Django到阿里云服务器教程示例
Jun 03 Python
完美解决python针对hdfs上传和下载的问题
Jun 05 Python
Python的控制结构之For、While、If循环问题
Jun 30 Python
Python类型转换的魔术方法详解
Dec 23 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
CodeIgniter常用知识点小结
2016/05/26 PHP
PHP实现一个简单url路由功能实例
2016/11/05 PHP
身份证号码前六位所代表的省,市,区, 以及地区编码下载
2007/04/12 Javascript
JQuery+Ajax无刷新分页的实例代码
2014/02/08 Javascript
node.js中的http.response.setHeader方法使用说明
2014/12/14 Javascript
分步解析JavaScript实现tab选项卡自动切换功能
2016/01/25 Javascript
jQuery UI仿淘宝搜索下拉列表功能
2017/01/10 Javascript
详解nodejs express下使用redis管理session
2017/04/24 NodeJs
vue.js获得当前元素的文字信息方法
2018/03/09 Javascript
解决Nodejs全局安装模块后找不到命令的问题
2018/05/15 NodeJs
Jquery实现无缝向上循环滚动列表的特效
2019/02/13 jQuery
微信小程序select下拉框实现效果
2019/05/15 Javascript
layui-tree实现Ajax异步请求后动态添加节点的方法
2019/09/23 Javascript
解决Layui数据表格的宽高问题
2019/09/28 Javascript
详解如何在JS代码中消灭for循环
2019/12/11 Javascript
Python实现视频下载功能
2017/03/14 Python
查看TensorFlow checkpoint文件中的变量名和对应值方法
2018/06/14 Python
python实现时间o(1)的最小栈的实例代码
2018/07/23 Python
Django 批量插入数据的实现方法
2020/01/12 Python
Python任务调度模块APScheduler使用
2020/04/15 Python
python GUI模拟实现计算器
2020/06/22 Python
python 第三方库paramiko的常用方式
2021/02/20 Python
资生堂美国官网:Shiseido美国
2016/09/02 全球购物
尤妮佳moony海外旗舰店:日本殿堂级纸尿裤品牌
2018/02/23 全球购物
货代行业个人求职简历的自我评价
2013/10/22 职场文书
大专毕业生自我鉴定
2013/11/21 职场文书
企业门卫岗位职责
2013/12/12 职场文书
旅游业大学生创业计划书
2014/01/31 职场文书
公务员转正鉴定材料
2014/02/11 职场文书
优秀的2014年两会精神解读
2014/03/17 职场文书
计算机软件专业求职信
2014/06/10 职场文书
兴趣班停课通知
2015/04/24 职场文书
2016年暑期见闻作文
2015/11/25 职场文书
解决hive中导入text文件遇到的坑
2021/04/07 Python
8个JS的reduce使用实例和reduce操作方式
2021/10/05 Javascript
springboot新建项目pom.xml文件第一行报错的解决
2022/01/18 Java/Android