码迷,mamicode.com
首页 > 其他好文 > 详细

1.实现一个用户登陆接口

时间:2017-11-10 20:20:05      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:try   utf-8   提示   login   readline   实现   stat   nbsp   coding   

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.实现一个用户登陆接口

标签:try   utf-8   提示   login   readline   实现   stat   nbsp   coding   

原文地址:http://www.cnblogs.com/changmingzhao/p/7815844.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!