装饰器添加模拟用户登陆页面(终极版)

基础的只支持在本地验证

###这是终极版本,基础版本需要输入两次验证,现在这个可以说是只验证一次把
##第五步是判断了
user,password = "caicai","13421731046"

def auth(auth_type):#在这之前的参数只能掉来第二层了func
    def ouder_wrapper(func):
        def wrapper(*args, **kwargs):
            if auth_type == "local":
                userInput = input("user:").strip()
                passwordInput = input("password:").strip()
                if userInput == user and passwordInput == password:
                    print("\033[32;1m Welcome to \033[0m")
                    ##用户登陆完成后应该要执行它之前的功能了
                    res = func(*args, **kwargs)
                    return res
                else:
                    exit("\033[31;1m login failure \033[0m")
            elif auth_type == "lapl":
                print("ww")
                print()
        return wrapper
    return ouder_wrapper


def index():
    print("welcome to index page")
###auth_type = "local"这个参数是直接传到第一次函数嵌套
@auth(auth_type = "local")##我现在要做只要登陆一次就行的(1)

def home():
    print("welcome to home page")
##又传进去一个参数,怎么搞定,嘻嘻,多加一层函数嵌套咯ouder_wrapper
@auth(auth_type = "lapl")##需要验证的加上,这里传的参数是用在判断上
def bbs():
    print("welcome to bbs page")


##调用
index()
home()
bbs()

 

转载于:https://www.cnblogs.com/ArtisticMonk/p/8932285.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值