16.python登录程序认证

版本一(单用户)

def auth_func(func):
    def wrapper(*args,**kwargs):
        user = input('用户名:').strip()
        passwd = input('密码').strip()
        if user=='bb' and passwd == '123':
            res = func(*args,**kwargs)
            return res
        else:
            print('用户名或密码错误')
    return wrapper


@auth_func
def index(name):
    print('%s welcome to index' %name)

@auth_func
def home(name):
    print('welcome to homepage %s' %name)

index('white')
home('hello')

版本二(多用户)

user_list = [
    {'name':'white','passwd':'123'},
    {'name': 'alex', 'passwd': '123'},
    {'name': 'leo', 'passwd': '123'},
    {'name': 'bai', 'passwd': '123'}
]


current_dic = {'name':None,'login':False}

def auth_func(func):
    def wrapper(*args,**kwargs):
        if current_dic['name'] and current_dic['login']:
            res = func(*args,**kwargs)
            return res
        user = input('用户名:').strip()
        passwd = input('密码').strip()

        for user_dic in user_list:
            if user==user_dic['name'] and passwd == user_dic['passwd']:
                current_dic['name']=user
                current_dic['login']=True
                res = func(*args,**kwargs)
                return res
        else:
            print('用户名或密码错误')
    return wrapper


@auth_func
def index(name):
    print('%s welcome to index' %name)

@auth_func
def home(name):
    print('welcome to homepage %s' %name)


index('white')
home('hello')

版本三(带参数验证功能的装饰器)

user_list=[
    {'name':'alex','passwd':'123'},
    {'name':'linhaifeng','passwd':'123'},
    {'name':'wupeiqi','passwd':'123'},
    {'name':'yuanhao','passwd':'123'},
]
current_dic={'username':None,'login':False}

def auth(auth_type='filedb'):
    def auth_func(func):
        def wrapper(*args,**kwargs):
            print('认证类型是',auth_type)
            if auth_type == 'filedb':
                if current_dic['username'] and current_dic['login']:
                    res = func(*args, **kwargs)
                    return res
                username=input('用户名:').strip()
                passwd=input('密码:').strip()
                for user_dic in user_list:
                    if username == user_dic['name'] and passwd == user_dic['passwd']:
                        current_dic['username']=username
                        current_dic['login']=True
                        res = func(*args, **kwargs)
                        return res
                else:
                    print('用户名或者密码错误')
            elif auth_type == 'ldap':
                print('鬼才特么会玩')
                res = func(*args, **kwargs)
                return res
            else:
                print('鬼才知道你用的什么认证方式')
                res = func(*args, **kwargs)
                return res

        return wrapper
    return auth_func

@auth(auth_type='filedb') #auth_func=auth(auth_type='filedb')-->@auth_func 附加了一个auth_type  --->index=auth_func(index)
def index():
    print('欢迎来到京东主页')

@auth(auth_type='ldap')
def home(name):
    print('欢迎回家%s' %name)
#
@auth(auth_type='sssssss')
def shopping_car(name):
    print('%s的购物车里有[%s,%s,%s]' %(name,'奶茶','妹妹','娃娃'))

# print('before-->',current_dic)
# index()
# print('after--->',current_dic)
# home('产品经理')
shopping_car('产品经理')