温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python怎么创建密码

发布时间:2021-11-23 17:03:17 来源:亿速云 阅读:169 作者:iii 栏目:编程语言

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

使用:
    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怎么创建密码”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI