Python3 Beginners Practice Case (4) can set the length and password complex level of the build password script
In the first combat of Python, I completed a script to generate the password. Original link: http://blog.csdn.net/fungleo/article/details/78753940
However, in the actual combat, the final script can only set one parameter, that is, length. I want to be able to do a more complete script, that not only can set the length, and can set the password complex level. In this way, we can meet the password that we need in different situations.
In addition, because passwords are randomly obtained in a dictionary, the characters are grouped together. This result may not meet the conditions we want, so you must check that if it is not satisfied, you will need to regenerate one until it is satisfied.
OK, set my program's goal: randomly generate a password can specify the length of the password can specify the complex level of the password
Simple: Composed of pure numbers general: number + lowercase letter + Capital Letter complex: number + lowercase letter + Capital Letter + punctuation code must meet specified complex level to output password minimum length is 4 digits, because complex mode must be composed of four elements
OK, I'm ready to implement this script after I've identified the target. Because I have learned the Argparse command-line parameter processing library, "argparse command line parameter library use", so processing command-line parameters for me to become very simple, I configured, just to put all the effort on the logical processing can be. Implementation Code
#!/usr/bin/env Python3 #-*-coding:utf-8-*-import random import argparse import re # using dictionaries and lengths to randomly generate a password def passwordmaker (Leng, dicts): res = ' Dictlen = Len (dicts)-1 for I in Range (Leng): Res + = dicts[random.randint (0, Dictlen)] Return res # check password rating def checkpassword (passwd): res = ' If Re.search (R ' \d ', passwd): res = ' s ' If Re.search (R ' [A-z] ', passwd) and Re.search (R ' [A-z] ', passwd): res = ' C ' if Re.searc H (R ' [_@!,. <>:;-=+/?] ', passwd): res = ' d ' return res # get password function def getpassword (leng,level,dicts) : res = PasswordMaker (Leng, dicts) # Check that the password meets the expected conditions, and if not, regenerate the If Checkpassword (res)!= Level:retur N GetPassword (Leng, level, dicts) return res # main function if __name__ = = "__main__": # set command line argument parser = Argparse. Argumentparser () parser.description= ' This program are used to generate simple or complex passwords ' Parser.add_argu ment ("-V", "--version", ACTIon= ' version ', version= '% (Prog) S 1.0 ') parser.add_argument (' length ', type=int, help= ' The length of the password (defaul
T 8) ', nargs= '? Group = Parser.add_mutually_exclusive_group () group.add_argument ("-S", "--simple", help= "The password is made up of PU Re numbers ", action=" Store_true ") group.add_argument ("-C ","--commonly ", help=" The password is made "" Letters (Default), action= "Store_true") group.add_argument ("-D", "--difficult", help= "The password is made" " ERS, letters, and punctuation ", action=" Store_true ") # Get command line argument results args = Parser.parse_args () # To determine whether the password length is a command-line setting or The default is 8 length = Args.length or 8 # If the password length is less than 4 prompts and exits the IF length < 4:parser.print_usage () PRI
NT (' error:the password length must to greater than 3 ') exit () # set three-level password-generated dictionaries sdict = ' 0123456789 '
cdict = ' abcdefghijkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789 ' ddict = cdict + ' _@!,.:;-=+/? '
# Print the final password according to the command line condition If Args.simple:print (GetPassword (length, ' s ', sdict)) Elif Args.difficult:print (GetPassword (Leng Th, ' d ', ddict) Else: # The default output is the general complexity of the password print (GetPassword (length, ' C ', cdict))
The results of the operation are as follows
Summary after completion
Main function
Before writing code, did not pay attention to this problem. But after seeing a picture today, I thought it was a rule to write the code.
It is also very easy to define a main function by simply adding the following code before the encapsulated code that you write:
if __name__ = = "__main__":
there's no research.
I always feel the following sentence:
If Re.search (R ' [A-z] ', passwd) and Re.search (R ' [A-z] ', passwd):
Can be optimized into a sentence is OK, but anyway did not think out. Anyway, it is not like this, high man to give me the advice of Bai:
If Re.search (R ' [A-za-z] ', passwd):
There is, the regular punctuation, I used [_@!,. <>:;-=+/?], this can be called a regular. Anyway I used [:p UNCT:] Failed to realize my idea. I hope to have expert advice.
All right, here's the summary.
This article by Fungleo Original, allow reprint, but reprint must retain the first link.