Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use python to realize user locking by incorrectly entering password

Shulou Source: shulou.com Published: 2022-06-02 18:29:32 10月26日 Update

This article mainly explains "how to use python to realize user locking of input wrong password". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian and go deep into it slowly to study and learn "how to use python to realize user locking of input wrong password" together!

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

2. Read whitelist file, assign content to a variable, and close.

3. Divide the variable with ":" and assign the first bit (index 0) to username and the second bit (index 1) to password.

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

5. Define a variable (t) to store the number of user inputs,

6. Cycle, when the cycle times are less than three times, continue to cycle, when the times are greater than three times, prompt input times greater than three times, the account is locked,

In the recycling process, input the user name, and judge whether the user name is in the blacklist. If yes, prompt "The account has been locked." If no longer in the blacklist, continue to judge in the white list. If the user name is in the list, continue to judge whether the password is correct.

#!/ usr/bin/env python

# -*- coding:utf-8 -*-

# @lynn

#Read login.user file, assign contents to variable login_f1, and close

f1 = open('login.user','r')

login_f1 = f1.read()

f1.close()

#Change the value of variable login_f1, delimited by ':'

#Extract the zeroth element and assign it to another variable, ruser

ruser = login_f1.strip().split(":")[0]

#Extract the first element and assign it to another variable rpwd

rpwd = login_f1.split(":")[1]

#Read blacklist file lock.user, assign contents to variable lock_f2, and close

f2 = open('lock.user','r')

lock_f2 = f2.readlines()

f2.close()

#Define a variable for counting

t = 0

#When t is less than 3, the loop is infinite.

while t < 3:

name = input("Please enter your account:")

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 password:")

if pwd == rpwd:

print("Welcome!% s" %name)

exit()

else:

print("Sorry! wrong password. ")

t += 1

else:

print("Sorry, the number of errors has reached 3, the account is locked! ")

f = open('lock.user', 'w')

f.write('%s' % name)

f.close()

exit()

Related Example 2 (Python 3.0):

# -*- coding:utf-8 -*-

MT5 Question Summary www.gendan5.com/mt5.html

#Requirements Simulate user login, more than three error locks are not allowed to log in

count = 0

#realname passwd

Real_Username = "test"

Real_Password = "test"

#Read the contents of the blacklist

f = open('black_user','r')

lock_file = f.read()

f.close()

Username = input ('Please input your username:')

#Judge whether the input user is in the blacklist, if yes, input password is not allowed

for i in range(1):

if lock_file == Username:

print ('sorry, your user has been locked, temporarily not allowed to use! ')

exit()

else:

continue

#Try to enter your password and count the number of times

for i in range(3):

Password = input("Please enter password:")

if Username == Real_Username and Password == Real_Password:

print("Login successful")

break

else:

print("Login failed")

count += 1

#If you enter the wrong password three times, you will be prompted to lock the username and black out the username.

if count == 3:

print("Sorry, you have entered the wrong password 3 times, your username will be locked")

f = open("black_user","w")

f.write("%s"%Username)

f.close()

Thank you for reading, the above is "how to use python to achieve the wrong password user lock" content, after the study of this article, I believe we have a deeper understanding of how to use python to achieve the wrong password user lock this problem, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Tags: User input password variable error content number of times user name blacklist blackname file loop list learning prompt login sorry element for index Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB NVidia Shulou Information Apple Shulou Tech Info