1.实现一个用户登陆接口
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1.实现一个用户登陆接口相关的知识,希望对你有一定的参考价值。
1.实现一个用户登陆接口
需求:
1.登陆前判断是否在黑名单中blacklist.txt,如存在提示用户被锁定
2.读取密码文件passwd.txt登陆,登录失败3次就锁定账号,加入黑名单,提示用户是否继续.
1 # -*- coding:utf-8 -*- 2 # Author: JACK ZHAO 3 # 需求: 4 # 实现一个用户登陆接口 5 # 1.登陆前判断是否在黑名单中blacklist.txt,如存在提示用户被锁定 6 # 2.读取密码文件passwd.txt登陆,登录失败3次就锁定账号,加入黑名单,提示用户是否继续 7 8 import os 9 import fileinput 10 11 status_sucess_flag=0 12 user_lock_flag=0 13 14 if not os.path.exists("blacklist.txt"): 15 file_create = open("blacklist.txt","w") #创建黑名单 16 file_create.close() 17 18 while True: 19 if status_sucess_flag == 1 or user_lock_flag == 1: 20 break 21 for i in range(3): 22 if status_sucess_flag == 1: 23 break 24 username = input("Usrname:") 25 password = input("Password:") 26 for line in fileinput.input("blacklist.txt"): #逐行读取 27 black_username = line.strip("\n") 28 if username == black_username: 29 print("The user is locked, please contact the administrator.") 30 user_lock_flag =1 31 break 32 if user_lock_flag == 1: 33 break 34 file_object = open(‘passwd.txt‘, ‘r‘) # 打开读取密码文件 35 while True: 36 line = file_object.readline().strip("\n") 37 _username = line.split(":")[0] 38 _password = line.split(":")[-1] #空格切割字符,取最后一个元素 39 if username == _username and password == _password: 40 print("Welcome {_name} login..".format(_name=username)) 41 status_sucess_flag = 1 42 break 43 if not line: 44 print("Login failed.Please check Username or Password.") 45 break 46 else: 47 print("Try more than 3 times,Has been locked.") 48 file_blacklist = open(‘blacklist.txt‘,‘a‘) #追加方式打开黑名单 49 file_blacklist.write(username) 50 file_blacklist.write("\n") 51 file_blacklist.close() 52 continuet_to_confirm = input("Continuet to confirm [y/n]:") 53 if continuet_to_confirm == "n": 54 print("You‘ve quit.") 55 break
以上是关于1.实现一个用户登陆接口的主要内容,如果未能解决你的问题,请参考以下文章