Python入门:装饰器案例3

import time

user,passwd="LIly","abc123"

def auth(auth_type):

  print("auth func:",auth_type)

  def outwrapper(func):

    def wrapper(*args,**kwargs):

      print("wrapper func args:", *args, **kwargs)

      if auth_type=="local":

        username=input("username:").strip()

        password=input("password:").strip()

        if username==user and password==passwd :

          print("\033[32:1mUser has passed authentication\033[0m")

          res=func(*args,**kwargs) #from home

          return res

        else:

          exit("\033[31;1mInvalid username or password\033[0m")

      elif auth_type=="ldap":

        print("搞毛线ldap,不会。。。。")

        

       return wrapper

    return outwrapper

 

def index():

  print("welcome to index page")

@auth(auth_type="local")

def home():

  print("welcome to home page")

  return "from home"

@auth(auth_type="ladp") # home = wrapper()

def bbs():

  print("welcome to bbs page")

 

index()

print(home())  #wrapper

bbs()

 

posted @ 2018-07-13 15:46  luckerzhang  阅读(147)  评论(0编辑  收藏  举报