|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
装饰器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()
复制代码 |
|