Python入门篇(七)之装饰器
需求:网站多页面,一个页面代表一个函数,网站谁都可以登录。现在需要在一些页面中增加验证功能登录 #!/usr/bin/python# _*_ coding:utf-8 _*_
# Aothr: Kim
user, passwd = 'alex', '123'
def auth(auth_type):
def out_wrapper(func):
def wrapper(*args,**kwargs):
if auth_type == "local":
print("Authing for file")
username = input("Login:").strip()
password = input("Password:").strip()
if username == user and password == passwd:
print("You has passed the authentication.")
func(*args,**kwargs)
else:
exit("Your username or password is invalid.")
elif auth_type == "ldap":
print("Authing for LDAP.")
print("You has passed the ldap authentication.")
func(*args, **kwargs)
return wrapper
return out_wrapper
def index():
print("welcome to index page")
@auth(auth_type="local")
def home():
print("welcome to home page")
@auth(auth_type="ldap")
def bbs():
print("welcome to bbs page")
#index()
Page_list = ["index","home","bbs"]
for i in Page_list:
print(i)
choice = input("Please enter the page you want to view:").strip()
if choice == "index":
index()
elif choice == "home":
home()
elif choice == "bbs":
bbs()
else:
exit("The page you selected does not exist.")
页:
[1]