当前位置:   article > 正文

python3装饰器之为函数添加验证功能(例:访问天猫商城登录账号密码)_使用装饰器,编程增加具有登录验证的函数

使用装饰器,编程增加具有登录验证的函数
  1. #创建账号数据库
  2. user_list = [{'username':'th','passwd':'baoweiluobu'},
  3. {'username':'my','passwd':'banjia'},
  4. {'username':'zy','passwd':'songsongsong'},
  5. ]
  6. #初始登录状态信息
  7. current_dic = {'username':None,'login':False}
  8. #带有验证功能的函数装饰器
  9. def auth_func(func):
  10. def wrapper(*args,**kwargs):
  11. if current_dic['username'] and current_dic['login']:
  12. res = func(*args,**kwargs)
  13. return res
  14. while True:
  15. username = input('请输入用户名')
  16. passwd = input('请输入密码')
  17. for user_dic in user_list:
  18. if username == user_dic['username'] and passwd == user_dic['passwd']:
  19. current_dic['username'] = username
  20. current_dic['login'] = True
  21. res = func(*args, **kwargs)
  22. return res
  23. else:
  24. print('用户名或密码错误')
  25. return wrapper
  26. @auth_func
  27. def index():
  28. print('欢迎来到天猫购物商城')
  29. @auth_func
  30. def shoppingcar():
  31. print('购物车:%s,%s,%s' %('葫芦娃','三明治','阿童木'))
  32. index()
  33. shoppingcar()

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/213281
推荐阅读
相关标签
  

闽ICP备14008679号