python3--Decorator Advanced Learning Edition

Source: Internet
Author: User
Tags ldap

__author__ = "Aaron Fan"
Import Time #导入time模块
user,passwd = ' Alex ', ' abc123 ' #用户名密码
def auth (auth_type): #创建auth函数, creating Auth_type parameters
#print ("auth func:", Auth_type) #打印形参auth_type的值
def outer_wrapper (func): #创建outer_wrapper函数, creating a func parameter
def wrapper (*args, **kwargs): #创建wrapper函数, creating non-fixed parameters *args,**kwargs
#print ("wrapper func args:", *args, **kwargs) #打印传进来的这些非固定参数的值
if Auth_type = = "Local": #如果auth函数中的形参auth_type的值等于local
Username = input ("username:"). Strip () #输入用户名
Password = input ("Password:"). Strip () #输入密码
If user = = Username and passwd = = Password: #如果用户名和密码全部正确
Print ("\033[32;1muser has passed authentication\033[0m") #以绿色的颜色打印一句登陆成功的提示语



res = func (*args, **kwargs) # from Home #这里主要是为了显示home () function return value: From home
Print ("---after Authenticaion") #打印一句话
return res #返回定义的res变量的值


else: #否则账号或者密码错误
Exit ("\033[31;1minvalid username or password\033[0m")
elif Auth_type = = "LDAP": #如果auth函数中的形参auth_type的值等于ldap
Print ("Engage the yarn LDAP, will not ....") ") #ldap的处理语句, here for the time being replaced with a print, remember that the following exercise is necessary to refine

Return wrapper #return Wrapper
return Outer_wrapper #return Outer_wrapper

# Index Home page, no user name password, can log in directly, so there is no need to decorate the device
def index ():
Print ("Welcome to Index page")

#home页面, need to match user login by local way
@auth (auth_type= "local") # home = wrapper ()
#注意 @auth represents Auth (), while @auth (auth_type= "local") represents Outer_wrapper ()
#所以这里其实是指 home = outer_wrapper (Home) = Wrapper
Def home ():
Print ("Welcome to Home Page")
Return "From Home"

#bbs页面, LDAP is required to match user login
@auth (auth_type= "LDAP")
Def BBS ():
Print ("Welcome to BBS page")

Index ()
Print (Home ()) #其实执行的是wrapper () This method displays the return value of home
#home () #其实执行的是wrapper () This does not show a return value
BBS () #其实执行的是wrapper ()

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.