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文件为空

转载于:https://www.cnblogs.com/madling/p/8399711.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值