装饰器3decorator

装饰器3decorator:包含参数传递

 1 user, passwd = "Flagon", "123"
 2 
 3 
 4 def auth(auth_type):
 5     print('auth func:', auth_type)
 6     def outer_wrapper(func):
 7         def wrapper(*args, **lwargs):
 8             print('wrapper func args:', *args, **lwargs)
 9             user_name = input('username:').strip()
10             password = input('password:').strip()
11             if auth_type == 'local':
12                 if user == user_name and passwd == password:
13                     print('\033[31:1mLogin success\033[0m', args)
14                     res = func(*args, **lwargs)  # 为了返回函数的值
15                     print('User has pass authentication!运行的函数名:', func.__name__)
16                     return res
17                 else:
18                     exit('\033[32:1mInvalid username or password\033[0m')
19             elif auth_type == 'ldap':
20                 res = func(*args, **lwargs)  # 为了返回函数的值
21                 print('User has pass authentication!运行的函数名:', func.__name__)
22                 print('搞毛线ldap,会不')
23                 return res
24         return wrapper
25     return outer_wrapper
26 
27 
28 def index():
29     print('Welcome to be here!')
30 
31 
32 @auth(auth_type='local')
33 def home():
34     print('Welcom to the homepage!')
35     return "From home"
36 
37 
38 @auth(auth_type='ldap')
39 def bbs():
40     print('Welcom to the bbs!')
41     return 'From bbs'
42 
43 
44 index()
45 home()
46 bbs()
View Code

 

posted @ 2020-09-22 10:35  龚志军Flagon  阅读(69)  评论(0编辑  收藏  举报