Decorator adds mock user landing page (Ultimate Edition)

Basic only supports local authentication

# ##This is the ultimate version. The basic version needs to be verified twice. Now this can be said to be verified only once 
. # #The fifth step is to judge the 
user,password = " caicai " , " 13421731046 "

def auth(auth_type): #The parameters before this can only fall to the second level func 
    def ouder_wrapper(func):
         def wrapper(*args, ** kwargs):
             if auth_type == " local " :
                userInput = input("user:").strip()
                passwordInput = input( " password: " ).strip()
                 if userInput == user and passwordInput == password:
                     print ( " \033[32;1m Welcome to \033[0m " )
                     # #After the user is logged in, it should be executed The function before it has 
                    res = func(*args, ** kwargs)
                     return res
                 else :
                    exit("\033[31;1m login failure \033[0m")
            elif auth_type == "lapl":
                print("ww")
                print()
        return wrapper
    return ouder_wrapper


def index():
     print ( " welcome to index page " )
 # ##auth_type = "local" This parameter is directly passed to the first function nesting 
@auth(auth_type = " local " ) # #I will do as long as Just log in once (1)

def home():
     print ( " welcome to home page " )
 # #Another parameter is passed in, how to do it, hee hee, add another layer of function nesting ouder_wrapper 
@auth(auth_type = " lapl " ) # #Requires verification Plus, the parameters passed here are used for judgment 
def bbs():
     print ( " welcome to bbs page " )


## Call 
index()
home()
bbs()

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324778124&siteId=291194637