装饰器3decorator
装饰器3decorator:包含参数传递user, passwd = "Flagon", "123"
def auth(auth_type):
print('auth func:', auth_type)
def outer_wrapper(func):
def wrapper(*args, **lwargs):
print('wrapper func args:', *args, **lwargs)
user_name = input('username:').strip()
password = input('password:').strip()
if auth_type == 'local':
if user == user_name and passwd == password:
print('\033[31:1mLogin success\033[0m', args)
res = func(*args, **lwargs)# 为了返回函数的值
print('User has pass authentication!运行的函数名:', func.__name__)
return res
else:
exit('\033[32:1mInvalid username or password\033[0m')
elif auth_type == 'ldap':
res = func(*args, **lwargs)# 为了返回函数的值
print('User has pass authentication!运行的函数名:', func.__name__)
print('搞毛线ldap,会不')
return res
return wrapper
return outer_wrapper
def index():
print('Welcome to be here!')
@auth(auth_type='local')
def home():
print('Welcom to the homepage!')
return "From home"
@auth(auth_type='ldap')
def bbs():
print('Welcom to the bbs!')
return 'From bbs'
index()
home()
bbs()
页:
[1]