python--------------装饰器练习

from functools import wraps
#1.编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件)
#要求登录成功一次后,后续的函数都无需再输入用户名和密码

def get_zhPwd():

    f = open('pwd','r',encoding='utf-8')
    content = f.readline().split(' ')
    return content
FLAG = False
def login1(func):

    def inner(*args,**kwargs):
        global FLAG
        '''登录'''
        if FLAG:
            print("已经登录过")
            ret = func(*args,**kwargs)
        else:

            print('请输入用户名和密码,点击回车确定!')
            content = get_zhPwd()
            zhanghao = content[0]
            pwd = int(content[1])

            if zhanghao == 'wuhen' and pwd == 123:
                FLAG = True
                print('登录成功')
                ret = func(*args,**kwargs)
            else:
                print('登录失败')
                FLAG = False
    return inner

@login1
def ceshi1():
    print('我是一号')
@login1
def ceshi2():
    print('我是二号')

ceshi1()
ceshi2()

猜你喜欢

转载自www.cnblogs.com/xiangrikuidebuluo/p/9484528.html
今日推荐