码迷,mamicode.com
首页 > 其他好文 > 详细

【每日一练】装饰器

时间:2018-09-29 00:47:02      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:三次   put   with open   账号密码   mes   验证   use   auth   color   

写一个用户登录认证函数,要求:

1.要求用户输入账号密码和txt中的用户名数据库对比;

2.要求用户有三次尝试机会;

3.要求用户登陆后,执行其他功能无需再验证;

txt数据:

{‘auth_name‘:‘user0‘, ‘auth_passwd‘:‘123‘}
{‘auth_name‘:‘user1‘, ‘auth_passwd‘:‘123‘}
{‘auth_name‘:‘user2‘, ‘auth_passwd‘:‘123‘}
{‘auth_name‘:‘user3‘, ‘auth_passwd‘:‘123‘}
 1 auth_state = {auth_name: None, auth_log: False}
 2 count = 3
 3 
 4 
 5 def auth(fun):
 6     def wap(*args, **kwargs):
 7         if auth_state[auth_name] and auth_state[auth_log]:
 8             res = fun(*args, **kwargs)
 9             return res
10         global count
11         while 1:
12             if count > 0:
13                 user = input(enter your auth name:\n).strip()
14                 passwd = input(enter your auth passwd:\n).strip()
15                 with open(test, r) as f:
16                     for line in f:
17                         line = eval(line)
18                         if line[auth_name] == user and line[auth_passwd] == passwd:
19                             auth_state[auth_name] = user
20                             auth_state[auth_log] = True
21                             res = fun(*args, **kwargs)
22                             return res
23                     else:
24                         print(user_name or passwd is wrong)
25                         count -= 1
26                         continue
27             else:
28                 print(three times faile, try later.)
29                 break
30 
31     return wap
32 
33 
34 @auth
35 def log():
36     print(welcome back)
37 
38 
39 @auth
40 def home():
41     print(welcome to home)
42 
43 
44 log()
45 home()

 

 

【每日一练】装饰器

标签:三次   put   with open   账号密码   mes   验证   use   auth   color   

原文地址:https://www.cnblogs.com/hrtian/p/9721532.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!