SoFunction
Updated on 2024-10-30

A python implementation of user lockout by entering an incorrect password.

I bring you the implementation of python to realize the user lock after multiple password input errors, and the specific process, so that you can better understand the process of running.

1. Create a new file for whitelisted users (users who have registered correctly in the format: username:password) and another file for blacklisted users (users who have entered their username incorrectly three times).

2. Read the whitelist file, assign the contents to a variable, and close it.

3. The variable will be divided by ":", divided out of the first (indexed as 0) assigned to username, the second (indexed as 1) assigned to password.

4. Read the blacklist file, assign the contents to a variable, and close it.

5. Define a variable (t) to hold the number of times the user has entered.

6. Carry out the cycle, when the number of cycles is less than three, continue to cycle, when the number of times is greater than three, will be prompted to enter a number of times greater than three, the account is locked, the

Then cycle, enter the user name, cycle to determine whether the user name in the blacklist, if in, then prompt "the account has been locked", if no longer in the blacklist, and then continue to determine the whitelist, if the user name and then the list, then continue to determine whether the password is correct.

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @lynn
 
# Read the file, assign the contents to the variable login_f1, and close the
f1 = open('','r')
login_f1 = ()
()
 
# Put the value of the variable login_f1, delimited by ':'
# Extract the 0th element and assign it to another variable ruser
ruser = login_f1.strip().split(":")[0]
# Extract the 1st element and assign it to another variable rpwd
rpwd = login_f1.split(":")[1]
 
# Read the blacklist file, assign the contents to the variable lock_f2, and close the
f2 = open('','r')
lock_f2 = ()
()
 
# Define a variable for counting
t = 0
 
# Infinite loop when t is less than 3.
while t < 3:
  name = input("Please enter account number:")
  for a in lock_f2:
    if name == a:
      print("Sorry! This account has been locked.")
      exit()
  for b in login_f1:
    if name == ruser:
      t = 0
      while t < 3:
        pwd = input("Please enter the password:")
        if pwd == rpwd:
          print("Welcome!%s" %name)
          exit()
        else:
          print("Sorry!wrong password.")
        t += 1
      else:
        print("Sorry, the number of errors reached 3, the account is locked!")
        f = open('', 'w')
        ('%s' % name)
        ()
      exit()

Related example two (python 3.0 ):

 

# -*- coding:utf-8 -*-
#Requirements simulate user logins, more than three error lockouts do not allow logins
 
 
count = 0
 
#realname passwd
Real_Username = "test"
Real_Password = "test"
 
#Read the contents of the blacklist
f = open('black_user','r')
lock_file = ()
()
 
Username = input('Please enter user name:')
 
# Determine whether the input user is in the blacklist, if so, do not allow the input of passwords
for i in range(1):
 if lock_file == Username:
 print('Sorry, your user is locked out for the moment!')
 exit()
 else:
 continue
 
# Attempts to enter a password and counts the number of times it is entered
for i in range(3):
 Password = input("Please enter the password:")
 if Username == Real_Username and Password == Real_Password:
 print("Login successful.")
 break
 else:
 print("Login failed.")
 count += 1
 
#If the wrong password is entered three times, it prompts a username lockout and the username is blacked out
if count == 3:
 print("Sorry, the number of incorrect passwords you have entered has reached 3, your username will be locked")
 f = open("black_user","w")
 ("%s"%Username)
 ()