Python代码之登录接口

# -*- coding:utf-8 -*-
#Author:Mr Gan
import sys

limit_time = 3
loggin_time = 0

while loggin_time < limit_time:
user = input('\033[31;1m username:\033[0m').strip()
with open('account_lock','r') as lock_name:
for lock_line in lock_name:
if user == lock_line.strip():
print('you have been locked')
if len(user) == 0:#判断是否输入空用户名
print('username is not none')
continue
password = input('\033[31;1m password: \033[0m').strip()
with open('account', 'r') as account_name:
mathch_flag = False #设置登录认证标志位

for user_line in account_name.readlines():
username, passwd = user_line.strip().split()
if (user,password) == (username,passwd):
print('%s login success'% user)
mathch_flag = True#设置登录认证标志位
break #跳出for循环

if mathch_flag == False and loggin_time < 2: #登录认证不匹配
print('username or password error,please try again')
loggin_time += 1
else: #登录认证成功
with open('account_lock', 'a+') as f:
f.write('%s\n'% user )
print('you have been locked ')
break
代码不足之处:用户被锁定时,只是第三次输入的用户名被锁定到文件中,不符合逻辑
      
posted @ 2017-05-08 12:57  MK3945  阅读(288)  评论(0编辑  收藏  举报