python怎么创建密码

本篇内容介绍了“python怎么创建密码”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

成都创新互联公司主营永仁网站建设的网络公司,主营网站建设方案,手机APP定制开发,永仁h5重庆小程序开发搭建,永仁网站营销推广欢迎永仁等地区企业咨询

使用:
    mkpass -l [password length] [-nu|-nl|-ns]
获取帮助:
    mkpass -h

  1. #!/usr/bin/env python3

  2. # _*_coding:utf-8_*_

  3. # Auth by raysuen

  4. import sys,string,re

  5. import random

  6. class MakePass(object):

  7.     rPassInfo={

  8.         "Length":8,

  9.         "NoUpper":False,

  10.         "NoLower":False,

  11.         "NoSysbol":False

  12.            }

  13.     def __init__(self,passlen=8,nu=False,nl=False,ns=False):

  14.         if passlen != 8:

  15.             self.rPassInfo["Length"] = passlen

  16.         if nu == True:

  17.             self.rPassInfo["NoUpper"] = True

  18.         if nl == True:

  19.             self.rPassInfo["NoLower"] = True

  20.         if ns == True:

  21.             self.rPassInfo["NoSysbol"] = True

  22.     def MkPass(self):

  23.         rPassStr={

  24.             "passStr":'',

  25.             "AllChar":[],

  26.             "pwIndex":[],

  27.             "num":0

  28.         }

  29.         if self.rPassInfo["NoUpper"] == False:

  30.             rPassStr["AllChar"].append(random.choice(string.ascii_uppercase))

  31.             rPassStr["passStr"] = rPassStr["passStr"]+''.join(string.ascii_lowercase)

  32.             rPassStr["num"] += 1

  33.         if self.rPassInfo["NoLower"] == False:

  34.             rPassStr["AllChar"].append(random.choice(string.ascii_lowercase))

  35.             rPassStr["passStr"] = rPassStr["passStr"] + ''.join(string.ascii_lowercase)

  36.             rPassStr["num"] += 1

  37.         if self.rPassInfo["NoSysbol"] == False:

  38.             rPassStr["AllChar"].append(random.choice("!@#$%&*()+.,"))

  39.             rPassStr["passStr"] = rPassStr["passStr"] + "!@#$%&*()+.,"

  40.             rPassStr["num"] += 1

  41.         rPassStr["passStr"] = rPassStr["passStr"] + ''.join(string.digits)

  42.         rPassStr["AllChar"].append(random.choice(string.digits))

  43.         rPassStr["num"] += 1

  44.         rPassStr["pwIndex"].extend(random.sample(range(1,self.rPassInfo["Length"]),rPassStr["num"]))

  45.         rPassStr["pwIndex"].sort()

  46.         n = 0

  47.         i = 0

  48.         res = ''

  49.         while i < self.rPassInfo["Length"]:

  50.             isMatch = False

  51.             for j in rPassStr["pwIndex"]:

  52.                 if i == j:

  53.                     res = res + ''.join(rPassStr["AllChar"][n])

  54.                     n += 1

  55.                     i += 1

  56.                     isMatch = True

  57.                     break

  58.             if isMatch == False:

  59.                 res = res + ''.join(random.choice(rPassStr["passStr"]))

  60.                 i += 1

  61.         return res

  62. def func_help():

  63.     print("""

  64.         Name:

  65.             mkpass      --make password

  66.         

  67.         Synopsis:

  68.             mkpass -l [password length] [-nu|-nl|-ns]

  69.             

  70.         Description:

  71.             -l      Specify a number for the length of password,defaul 8

  72.             -nu     Don't use upper

  73.             -nl     Don't use lower

  74.             -ns     Don't use special character

  75.             -h      Get help

  76.     """)

  77. if __name__=='__main__':

  78.     i = 1

  79.     mp = MakePass()

  80.     while i < len(sys.argv):

  81.         if sys.argv[i] == '-l': #-l means length

  82.             if i+1 > len(sys.argv):

  83.                 print("The value of -l must be specified!!!")

  84.                 exit(1)

  85.             elif re.match("^-", sys.argv[i+1]) != None: #判断下个参数是否以-开头

  86.                 print("The value of -l must be specified!!!")

  87.                 exit(1)

  88.             elif sys.argv[i+1].isdecimal() == False: #判断-l下一个参数是否为数字

  89.                 print("The value of -l must be digits")

  90.                 exit(1)

  91.             i += 1

  92.             mp.rPassInfo["Length"] = int(sys.argv[i]) #获取-l参数的value

  93.         elif sys.argv[i] == '-nu': #-nu 指定不适用大写字符

  94.             mp.rPassInfo["NoUpper"] = True

  95.         elif sys.argv[i] == '-nl': #-nu 指定不适用小写字符

  96.             mp.rPassInfo["NoLower"] = True

  97.         elif sys.argv[i] == '-ns': #-nu 指定不适用特殊字符

  98.             mp.rPassInfo["NoSysbol"] = True

  99.         elif sys.argv[i] == '-h':

  100.             func_help()

  101.             exit(0)

  102.         i += 1

  103.     print(mp.MkPass())

“python怎么创建密码”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!


网页题目:python怎么创建密码
链接分享:http://myzitong.com/article/jpcsei.html