成都创新互联网站制作重庆分公司

python怎么创建密码-创新互联

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

创新互联公司是一家以网络技术公司,为中小企业提供网站维护、成都做网站、网站设计、网站备案、服务器租用、域名申请、软件开发、微信小程序等企业互联网相关业务,是一家有着丰富的互联网运营推广经验的科技公司,有着多年的网站建站经验,致力于帮助中小企业在互联网让打出自已的品牌和口碑,让企业在互联网上打开一个面向全国乃至全球的业务窗口:建站欢迎联系:18982081108

使用:
    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://cxhlcq.com/article/dghjgo.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部