python装饰器实现用户密码认证(简单初形)
2024-09-06 13:30:40
import time
current_user={'user':None}
def auth(engine = 'file'):
def deco(func): #func=最初始的index和最初始的home
def wrapper(*args,**kwargs):
if current_user['user']:
#如果成立说明current_user里面已经有用户信息,说明用户已经登录过,满足条件
#即会执行调用index home函数
res = func(*args, **kwargs) #调用最原始的index 同样也调用最原始的home,
return res user=input('输入你的用户名:').strip()
pwd=input('输入你的密码:').strip()
if engine == 'file':
#基于文件的认证
if user == 'yangzhizong' and pwd == '123':
print('认证成功')
#记录用户登录状态
current_user['user']=user
res = func(*args, **kwargs)
return res
else:
print('用户名密码错误') elif engine == 'mysql':
print('基于mysql的认证')
elif engine == 'idap':
print('基于idap的认证')
else:
print('无法识别认证来源') return wrapper
return deco # @timmer 2层是 3层此时可以理解为:@auth(engine = 'file')
#可以理解为上面这种情况
# x=auth(engine='file')
# @x
@auth(engine='file') #拿到了deco的返回值,最后调用index加(),输出结果 #deco #index=deco(index) #index=wrapper
def index():
time.sleep(1)
print('welcome to index page') @auth(engine='file')
def home(name):
print('welcome %s to home page'%name )
time.sleep(0.5) index()
home('yangzhizong')
最新文章
- 回忆:#define的用法
- hadoop集群安装无密码登录
- ISPA
- 慕课网-Java入门第一季-6-10 练习题
- [Unity] 精灵动画制作中需要注意的一些问题
- ThreadPool原理介绍
- Shallow Size 和 Retained Size
- 图像处理工具包ImagXpress中如何定义查看器的属性
- 浅析 GRUB 如何加载 linux kernel
- 【原】 Spark中Worker源码分析(二)
- Linq to Sql语法及实例大全
- asp.net 的那点事(1、当用户在浏览器地址栏输入了网址后,发生了什么?)
- 交互设计师谈颠覆式创新 | Think different
- include和 merge
- 学习Sass之安装篇
- ABP官方文档翻译 4.1 应用服务
- 201421123042 《Java程序设计》第6周学习总结
- php使用protobuf3
- jquery 取id模糊查询
- dRMT: Disaggregated Programmable Switching, SIGCOMM17