Function closure simulates session

According to the problem of the previous authentication function, the problem to be solved is that you only need to log in once, that is, the user name and password after logging in once can be saved for other functions to use --> global variables

user_dic = { " user_name " :None, " login " :False}   #Global variables note that None and False must be capitalized


def auth_func(func):
     def wrapped(*args,** kwargs):
         if user_dic[ " user_name " ] and user_dic[ " login " ] :   #This is the opposite of global variables such as if login is True and user_name is not equal to none 
            res = func(*args, **kwargs) #The            opposite result means that the function has been logged in and directly execute the function 
            return res
        user_name = input( " Please enter your username " ).strip()
        passwd = input( " Please enter your password " ).strip()
         if user_name == " sb "  and passwd == " 123 " :
            user_dic[ " user_name " ] = user_name      #This line and the next line are to modify the global variable 
            user_dic[ " login " ] = True
            res = func(*args,** kwargs)
             return res
         else :
             print ( " Incorrect username or password " )
     return wrapped


@auth_func
def index():
     print ( " Welcome to JD.com " )

@auth_func
def home(name):
     print ( " Dear %s, welcome home " % name)

@auth_func
def shopping_car(name):
     print ( " %s has %s in the shopping cart, %s " %(name, " tt " , " doll " ))

index()
home( " Dragon Brother " )
shopping_car( " Dragon Brother " )
#Upgraded version this time is a list of lists with a for loop to traverse in turn  

user_list= [ {'name':'alex','passwd':'123'}, { ' name ' : ' linhaifeng ' , ' passwd ' : ' 123 ' }, {'name':'wupeiqi','passwd':'123'}, {'name':'yuanhao','passwd':'123'},] current_dic = {"user_name":None,"login":False} def auth_func(func): def wrapped(*args,**kwargs): if current_dic["user_name"] and current_dic["login"] : res = func(*args, **kwargs) return res user_name = input( " Please enter your username " ).strip() passwd = input( " Please enter your password " ).strip() for user_dic in user_list: if user_name == user_dic[ " name " ] and passwd == user_dic[ " passwd " ]: #Use a for loop to traverse the user name list to see the input Is the match current_dic[ " user_name " ] = user_name current_dic["login"] = True res = func(*args, ** kwargs) return res else : #The else here is forwarded by a space because it needs to wait for the user to input # A few times until the for loop traverses all the user names before reporting an error, instead of reporting an error once input print ( " Incorrect username or password " ) return wrapped @auth_func def index(): print ( " Welcome to JD.com " ) @auth_func def home(name): print ( " Dear %s, welcome home " % name) @auth_func def shopping_car(name): print ( " %s has %s in the shopping cart, %s " %(name, " tt " , " doll " )) index() home( " Dragon Brother " ) shopping_car( " Dragon Brother " )

 

Guess you like

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