How does python create a password
This article introduces the knowledge of "how to create a password in python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Use:
Mkpass-l [password length] [- nu |-nl |-ns]
Get help:
Mkpass-h
#! / usr/bin/env python3
# _ * _ coding:utf-8_*_
# Auth by raysuen
Import sys,string,re
Import random
Class MakePass (object):
RPassInfo= {
"Length": 8
"NoUpper": False
"NoLower": False
"NoSysbol": False
}
Def _ init__ (self,passlen=8,nu=False,nl=False,ns=False):
If passlen! = 8:
Self.rPassInfo ["Length"] = passlen
If nu = = True:
Self.rPassInfo ["NoUpper"] = True
If nl = = True:
Self.rPassInfo ["NoLower"] = True
If ns = = True:
Self.rPassInfo ["NoSysbol"] = True
Def MkPass (self):
RPassStr= {
"passStr":'
"AllChar": []
"pwIndex": []
"num": 0
}
If self.rPassInfo ["NoUpper"] = = False:
RPassStr ["AllChar"] .append (random.choice (string.ascii_uppercase))
RPassStr ["passStr"] = rPassStr ["passStr"] + '.join (string.ascii_lowercase)
RPassStr ["num"] + = 1
If self.rPassInfo ["NoLower"] = = False:
RPassStr ["AllChar"] .append (random.choice (string.ascii_lowercase))
RPassStr ["passStr"] = rPassStr ["passStr"] + '.join (string.ascii_lowercase)
RPassStr ["num"] + = 1
If self.rPassInfo ["NoSysbol"] = = False:
RPassStr ["AllChar"] .append (random.choice ("! @ # $% & * () +.,"))
RPassStr ["passStr"] = rPassStr ["passStr"] + "! @ # $% & * () +."
RPassStr ["num"] + = 1
RPassStr ["passStr"] = rPassStr ["passStr"] + '.join (string.digits)
RPassStr ["AllChar"] .append (random.choice (string.digits))
RPassStr ["num"] + = 1
RPassStr ["pwIndex"] .extend (random.sample (range (1memself.rPassInfo ["Length"]), rPassStr ["num"]))
RPassStr ["pwIndex"] .sort ()
N = 0
I = 0
Res =''
While i
< self.rPassInfo["Length"]: isMatch = False for j in rPassStr["pwIndex"]: if i == j: res = res + ''.join(rPassStr["AllChar"][n]) n += 1 i += 1 isMatch = True break if isMatch == False: res = res + ''.join(random.choice(rPassStr["passStr"])) i += 1 return res def func_help(): print(""" Name: mkpass --make password Synopsis: mkpass -l [password length] [-nu|-nl|-ns] Description: -l Specify a number for the length of password,defaul 8 -nu Don't use upper -nl Don't use lower -ns Don't use special character -h Get help """) if __name__=='__main__': i = 1 mp = MakePass() while i < len(sys.argv): if sys.argv[i] == '-l': #-l means length if i+1 >Len (sys.argv):
Print ("The value of-l must be specialties!")
Exit (1)
Elif re.match ("^ -", sys.argv [iTune1])! = None: # determine whether the next parameter starts with-
Print ("The value of-l must be specialties!")
Exit (1)
Elif sys.argv [I + 1] .isdecimal () = = False: # determine whether-l the next parameter is a number
Print ("The value of-l must be digits")
Exit (1)
I + = 1
Mp.rPassInfo ["Length"] = int (sys.argv [I]) # get the value of the-l parameter
Elif sys.argv [I] ='- nu': #-nu specifies that uppercase characters are not applicable
Mp.rPassInfo ["NoUpper"] = True
Elif sys.argv [I] ='- nl': #-nu specifies that lowercase characters are not applicable
Mp.rPassInfo ["NoLower"] = True
Elif sys.argv [I] ='- ns': #-nu specifies that special characters are not applicable
Mp.rPassInfo ["NoSysbol"] = True
Elif sys.argv [I] ='- hags:
Func_help ()
Exit (0)
I + = 1
Print (mp.MkPass ())
That's all for "how to create a password for python". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!