python_64_装饰器7

# home密码认证是本地文件认证,bbs密码认证是远程ldat认证
import time
user, passwd = 'qi', '123'
def auth(auth_type):
    print('auth func:', auth_type)
    def outwrapper(func):
        def wrappper(*args, **kwargs):
            print('wrappper func args:', *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)  # from home
                    print('---after authentiction')
                    return res
                else:
                    exit('\033[31;1mInvalid username or password\033[0m')
            elif auth_type == 'ldap':
                print('搞毛线ldap,不会。。。')
        return wrappper
    return outwrapper
def index():  # 首页(不需要登录)
    print('Welcome to index page!')
@auth(auth_type='local')  # home=wrapper()
def home():  # 主页(需要登录)
    print('Welcome to home page!')
    return 'from home'
@auth(auth_type='ldap')
def bbs():  # 论坛页(不需要登录 )
    print('Welcome to bbs page!')

index()
print(home())  # 调用home相当于调用wrapper()
bbs()

  

posted @ 2018-01-30 00:11  耐烦不急  阅读(169)  评论(0编辑  收藏  举报