python实例31[生成随即的密码]

代码:

import random
import string
import time

# strong.high = 3  #random for the whole passwd
#storng.middle = 2  # include one special sign
#strong.ow = 1  # just include characters or digits

def mkpassByRandom(size=8, strong = 2):
    chars = []
    chars.extend([i for i in string.ascii_letters])
    chars.extend([i for i in string.digits])
    chars.extend([i for i in ‘\‘"[email protected]#$%&*()-_=+[{}]~^,<.>;:/?‘])
    
    passwd = ‘‘
    strong = int(strong)
    
    if (strong <= 1) :
     for i in range(size):
       passwd += chars[random.randint(0,len(string.ascii_letters + string.digits) - 1)]
       random.seed = int(time.time())
    elif(strong == 2):
       newpasswd = ‘‘
       for i in range(size - 1):
         newpasswd +=chars[random.randint(0,len(string.ascii_letters + string.digits) - 1)]
         random.seed = int(time.time())
       newpasswd += chars[random.randint(len(string.ascii_letters + string.digits) , len(chars) - 1)]
       ll = [ch for ch in newpasswd]
       random.shuffle(ll)
       for l in ll:
         passwd += l
    elif(strong >=3):
      for i in range(size):
        passwd += chars[random.randint(0,  len(chars) - 1)]
        random.seed = int(time.time())
        random.shuffle(chars)
    else:
      pass
     
    return passwd
  
def rule1(ch):
    rulesdict = { ‘o‘: 0, ‘i‘:‘!‘, ‘b‘:8, ‘p‘:‘P‘, ‘m‘:‘M‘}
    newch = ch
    
    if ch in rulesdict.keys():
      newch = rulesdict[ch]
    return newch

def rule2(ch):
  if(ch.isupper()):
    return ch.lower()
  elif(ch.islower()):
    return ch.upper()
  return ch

def mkpassByRules(passwd, *rules ):
  if (passwd == "" or len(rules) == 0):
    return passwd
  
  newpasswd = ""
  for c in passwd:
    r = random.randint(0, len(rules) - 1)
    ch = (rules[r])(c)
    newpasswd += str(ch)

return newpasswd

def mkpass(size = 8, strong = 2, initpasswd = ""):
  if ( not initpasswd == ""):
    return mkpassByRules(initpasswd, rule1, rule2)
  else:
    return mkpassByRandom(size,strong)

print( mkpass(initpasswd = "Password123"))
print( mkpass(strong = 1))
print( mkpass(strong = 2))
print( mkpass(strong = 3))

参考:http://code.activestate.com/recipes/577339-random-passwords/

python实例31[生成随即的密码]

时间: 2024-08-29 13:58:08

python实例31[生成随即的密码]的相关文章

python实例31[列出目录下所有的文件到txt]

代码: (使用os.listdir) import os def ListFilesToTxt(dir,file,wildcard,recursion):    exts = wildcard.split(" ")    files = os.listdir(dir)    for name in files:        fullname=os.path.join(dir,name)        if(os.path.isdir(fullname) & recursion

Python实例31[批量对目录下文件重命名]

经常会遇到下载的文件或电子书,名字中间都包含了一些网址信息,实际使用中由于名字太长不方便,下面的脚本使用正则表达式来对目录下的所有文件重命名:例如: 修改前:[大家网]Mac OS X for Unix Geeks[www.TopSage.com].mobi修改后:Mac OS X for Unix Geeks.mobi python代码如下 import osimport re def rename_dir(dir,regex,f):  if not os.path.isdir(dir) or

python实例:实现用户名,密码,验证码输入登录网页系统

python+selenium环境准备: python 自行安装 一.安装selenium:pip install selenium 二.下载谷歌浏览器驱动 1.去http://chromedriver.storage.googleapis.com/index.html下载chromedriver.exe(根据chrome的版本下载对应的) 2.将下载好的chromedriver.exe解压后放到指定目录(我一般放在代码目录下) 三,安装第三方库,可参考文章:https://www.cnblog

python和java生成随即序列

我指的随机序列就是好像洗牌那样把一个序列打散 看看java的实现 public static int[] random_serial(int limit) { int[] result = new int[limit]; for (int i = 0; i < limit; i++) result[i] = i + 1;//这里可以调整 int w; Random rand = new Random(); for (int i = limit - 1; i > 0; i--) { w = ra

【转载】python实例手册

今天西爬虫的时候遇到了问题,在网上不停地查找资料,居然碰到两篇好文章: 1.python实例手册   作者:没头脑的土豆 另一篇在这:shell实例手册 python实例手册 #encoding:utf8 # 设定编码-支持中文 0说明 手册制作: 雪松 更新日期: 2013-12-19 欢迎系统运维加入Q群: 198173206 # 加群请回答问题 请使用"notepad++"打开此文档,"alt+0"将函数折叠后方便查阅 请勿删除信息,转载请说明出处,抵制不道德

python实例手册

python实例手册 #encoding:utf8 # 设定编码-支持中文 0说明 手册制作: 雪松 更新日期: 2013-12-19 欢迎系统运维加入Q群: 198173206 # 加群请回答问题 请使用"notepad++"打开此文档,"alt+0"将函数折叠后方便查阅 请勿删除信息,转载请说明出处,抵制不道德行为. 错误在所难免,还望指正! # python实例手册下载地址: http://hi.baidu.com/quanzhou722/item/cf447

如何在 SQL Server 2005 实例之间传输登录和密码

简介 0" style="box-sizing: border-box; outline: none; margin-right: auto; margin-left: auto; max-width: 1600px; width: 761.391px;"> 本文介绍如何在不同服务器上的 Microsoft SQL Server 2005 实例之间传输登录和密码. 有关如何在其他版本的 SQL Server 实例之间传输登录和密码的更多信息,请单击下面的文章编号,以查看

Python实例 -- 爬虫

1 #coding="utf-8" 2 3 import urllib2 4 import re 5 import threading 6 import time 7 8 """ 9 抓取代理发布页的ip和port10 http://www.xici.net.co/nn/%d 11 """ 12 13 proxylist = [] 14 15 16 def get_proxy_from_cnproxy(): 17 global

第一个python实例--监控cpu

1 #第一个python实例:监控cpu 2 #/bin/bash/env Python 3 from __future__ import print_function 4 from collections import OrderedDict 5 import pprint 6 7 def CPUinfo(): 8 ''' Return the information in /proc/CPUinfo 9 as a dictionary in the following format: 10