__author__ ="Alex Li"
ImportTime#Import TimeModule
USER,PASSWD =' Alex ',' abc123 '#User name password
defAuth (auth_type):#CreateAuthfunction, creatingAuth_typeFormal parameters
#print ("auth func:", Auth_type) #Print Formal parametersAuth_typethe value
defOuter_wrapper (func):#CreateOuter_wrapperfunction, creatingfuncFormal parameters
defWrapper (*args, **kwargs):#Createwrapperfunctions, creating non-fixed parameters*args,**kwargs
#print ("wrapper func args:", *args, **kwargs) #print the values of these non-fixed parameters passed in
ifAuth_type = ="Local":#ifAuthformal parameters in a functionAuth_typeThe value is equal toLocal
Username =input("Username:"). Strip ()#Enter user name
Password =input("Password:"). Strip ()#Enter password
ifuser = = Username andpasswd = = Password:#if the user name and password are all correct
Print("\033[32;1muser has passed authentication\033[0m])#Print a successful sign in green color
res = func (*args, **kwargs)# from Home #The main purpose here is to showHome ()in the functionreturnvalues:From Home
Print("---after Authenticaion")#Print a Word
returnRes#returns the definedResthe value of the variable
Else:#otherwise the account or password is wrong
Exit("\033[31;1minvalid username or password\033[0m])
elifAuth_type = ="LDAP":#ifAuthformal parameters in a functionAuth_typeThe value is equal toLDAP
Print("KnittingLDAP,not .... ")#ldapthe processing statement, here for the time being a first sentencePrintinstead, remember that the following exercises are needed to refine
returnWrapper#return Wrapper
returnOuter_wrapper#return Outer_wrapper
# Indexhome page, do not need user name password, you can log in directly, so there is no need to decorate the device
defIndex ():
Print("Welcome to index page")
#homepage, you need to passLocalway to match user login
@Auth(Auth_type="Local")# home = wrapper ()
#Note@authRepresentativeauth (),and@auth (auth_type= "local")RepresentativeOuter_wrapper ()
#so here's actually the pointhome = outer_wrapper (Home) = Wrapper
defHome ():
Print("Welcome to Home Page")
return"From Home"
#bbspage, you need to passLDAPway to match user login
@Auth(Auth_type="LDAP")
defBBS ():
Print("Welcome to BBS Page")
Index ()
Print(Home ())#in fact, the implementation iswrapper ()This method will showHomeof thereturnvalue
#home () #in fact, the implementation iswrapper ()this won't show .returnvalue
BBS ()#in fact, the implementation iswrapper ()
This article is from the "Jinzhuzhechi" blog, make sure to keep this source http://fanheng.blog.51cto.com/974941/1881590
python3--Decorator Advanced Learning Edition