Python Demo Login-Verify user name and password
1. If the input error, the output user name or password error;
2. If successful, the output is welcome, xxx!
3. Cumulative error three times in three minutes the default user is locked, and after three minutes the automatic unlocking continues to authenticate.
The simple code is as follows:
Import Getpass
Import datetime
User_pwd_dict = {}
User_login_sum_dict = {}
With open (' User_pwd ', ' r ') as obj: # Read user name password file (file format at the end of this document);
While True: # infinite Loop, when the input user name and password are correct before exiting the loop;
Name = input ("\nplease input your Username:")
PWD = getpass._raw_input ("Please input your password:")
For line in obj: # traverse file to read out user name and password, generate user_pwd_dict list;
New_line = Line.strip (' \ n ')
Line_list = New_line.split (")
User_pwd_dict[line_list[0]] = line_list[1]
If name in User_pwd_dict.keys (): # 1.1 Determine the user name if the user name exists to perform the following
if pwd = = User_pwd_dict[name]: # 2.1 Determine password and correct password
If name in User_login_sum_dict.keys () and
User_login_sum_dict.get (name) [0]==1:
# 3.1 Determine the number of user errors, if it is 3 times, prompted to be locked
End_time=datetime.datetime.now ()
Dif_time = (end_time-start_time). seconds
If Dif_time <: # 4.1 Determine the time interval to be locked, if not more than 3 minutes
(180s) prompt to be locked (reduces time for easy testing)
Print (' The user%s has been locked for more than 3 times, please retry '% name ' later)
else: # 4.2 If more than 3 minutes (180s) Prompt login successful and exit the program
Print (' \ n Welcome%s '% name)
Break
else: # 3.2 Prompt for login success and exit program if password is correct and not locked
Print (' \ n Welcome%s '% name)
Break
else: # 2.2 Password error do the following
If name in User_login_sum_dict.keys (): # 5.1 Determine if the user name is in the locked dictionary
If User_login_sum_dict.get (name) [0]==1: # 6.1 Determines the number of times the user has lost the error,
if it is 3 times perform the following
End_time=datetime.datetime.now ()
Dif_time = (end_time-start_time). seconds
If Dif_time <: # 7.1 Error 3 times and not exceeding 3 minutes (180s) Prompt is locked,
continue the cycle (shorten the time for easy testing)
Print (' The user%s has been locked for more than 3 times, please retry '% name ' later)
else: # 7.2 Error 3 times more than 3 minutes (180s),
Prompt for password error and re-assign value, continue looping
Print (' \ n password isn ' t right ')
Start_time = Datetime.datetime.now ()
User_login_sum_dict[name]=[0,start_time]
Print (User_login_sum_dict[name])
else: # 6.2 The user has lost 3 or less, perform the following
End_time=datetime.datetime.now ()
Dif_time = (end_time-start_time). seconds
If Dif_time <: # 8.1 The user has lost 3 times within 3 minutes
prompt password error and accumulate error number plus 1 times, continue to cycle
Print (' \ n password isn ' t right ')
User_login_sum_dict[name][0]+=1
else: # 8.2 The user has lost 3 or more times and more than 3 minutes,
Prompt for password error and re-assign value, continue looping
Print (' \ n password isn ' t right ')
Start_time = Datetime.datetime.now ()
User_login_sum_dict[name]=[0,start_time]
Print (User_login_sum_dict[name])
else: # 5.2 Prompt If the user name is not in the locked dictionary
the password is incorrect and the initial value is assigned to continue looping
Print (' \ n password isn ' t right ')
Start_time = Datetime.datetime.now ()
User_login_sum_dict[name]=[0,start_time]
Print (User_login_sum_dict[name])
else: # 1.2 If the user name is not in the dictionary and the user name does not exist, continue looping
Print (' \ n this username doesn ' t exist! ')
-----------------------User name password file format is as follows------------------------
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/79/59/wKiom1aPGn2ANLnjAAAZRGzbTgc071.png "title=" User_ PWD "alt=" Wkiom1apgn2anlnjaaazrgzbtgc071.png "/>
Note: The level is limited, this blog if there is no consideration of the place also please teach to learn together, thank you.
This article is from the "a myriad of simple pleasures of technology to enjoy" blog, please be sure to keep this source http://51enjoy.blog.51cto.com/8393791/1732774
Python Analog System Login Interface