装饰器3


import time

user,passwd = 'lian','abc123'
def auth(auth_type):
def outer_wrapper(func):
def wrapper(*args,**kwargs):
if 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)
print('=============')
return res
else:
exit("\033[32;1mInvalid username or password\033[0m")
elif auth_type == "ldap":
print('ldap++++++')
return wrapper
return outer_wrapper
def index():
print('welcome to index page')
@auth(auth_type="local")#home = wraper()
def home():
print('welcome to home page')
return "welcome"
@auth(auth_type="ldap")#
def bbs():
print("welcome to bbs page")

index()
print(home())
bbs()






welcome to index page
Username:lian
Password:abc123
User has passed authentication
welcome to home page
=============
welcome
ldap++++++

posted @ 2018-11-07 17:04  rongye  阅读(128)  评论(0编辑  收藏  举报