__author__ ="Aaron Fan"
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/1881593
Python3--decorator Advanced Learning Edition