python编写登录接口 (day1)

需求说明:

  用户登录,对密码进行三次校验,三次均失败后,将锁定用户名,不允许登录。

代码实现:

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 #Author:Madling Xu
 4 
 5 
 6 # _id = "xu"
 7 # _password = "123"
 8 
 9 # 读取黑名单的内容
10 f = open('black_id','r+')  #打开account_lock文件,权限是读取更新,并赋值给lock_f变量
11 lock_file = f.readlines() #使用.readlines的方法逐行读取account_lock文件,并赋值给lock_list变量
12 # print(lock_file)
13 # f.close()
14 count=0
15 #验证id和密码是否匹配,如三次填写错误,id将会被锁定
16 # 读取账号密码
17 user_file = open("account", "r")
18 user_list = user_file.readlines()
19 
20 for user_line in user_list:         #使用for循环读取account的内容
21     (user,passwd) = user_line.strip('\n').split(",") #分别获取账号和密码信息
22     while count < 3:
23         id = input("ID:")
24         # 判断id是否在黑名单中
25         for lock_line in lock_file:
26             lock_line = lock_line.strip('\n')
27             if lock_line == id:
28                 print("Sorry,your ID has been locked up!")
29                 exit()
30         password = input("PASSWORD:")
31         if id == user and password == passwd:
32             print("Successful certification, welcome back!",id) #认证成功,欢迎回来
33             exit()
34         else:
35             print("Authentication error, reinput!")#认证错误,重新输入
36             count +=1
37     else:
38         print("The number of error is too much and has been locked!")#错误次数过多,已锁定
39         # f = open('black_id','r+')
40         f.write(id+'\n')  #用户名密码输入次数超过3次的用户添加到 black_id 文件中
41         f.close()  #关闭 f 文件
42         exit()
login.py
xu,123
fengzi,qwe111
fengzi97,qwe123
account

black_id文件为空

posted @ 2018-02-01 16:26  Madling  阅读(159)  评论(0编辑  收藏  举报