190327 Python登录接口

时间:2022-09-21 16:28:44
 #!Author:John
# _*_ coding: utf-8 _*_
#编写登录接口
#输入用户名密码
#认证成功后显示欢迎信息
#输错三次后锁定
import sys, os, getpass limit = 3
count = 0
account_file = "account.txt"
lock_file = "locked.txt" while count < limit:
username = input("Please input username:")
# 打开文件后第一步输入用户名,打开lock文件,检查改用户是否已经被锁定 f1 = open(lock_file,'r')
# r后面不能加b,加b是以bytes类型打开,输入的用户名、密码是字符串str和bytes不匹配
for line in f1.readlines():
if username == line.strip():
sys.exit("User %s has been locked!"%username)
f1.close()
password = input("Please input password:") f = open(account_file, 'r')
match_flag = False
for line in f.readlines():
user,pwd=line.strip().split() # 这里的知识点需要掌握
if user==username and pwd == password:
print(username,"has match successfully!")
match_flag=True
break
f.close()
if match_flag==False:
print("Username or password is incorrect!")
count+=1
else:
print("Welcome login Python Learning system!")
break
else:
print("Your account has been locked!")
f1=open(lock_file,"a")
f1.write(username+'\n')
f1.close()

190327 Python登录接口

190327 Python登录接口