Python3--decorator Advanced Learning Edition

Source: Internet
Author: User
Tags ldap


__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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.