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('33[32;1mUser has passed authentication33[0m')
                    res = func(*args, **kwargs)  # from home
                    print('---after authentiction')
                    return res
                else:
                    exit('33[31;1mInvalid username or password33[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()

  

原文地址:https://www.cnblogs.com/tianqizhi/p/8380137.html