如果登录尝试次数过多,则阻止登录



我想"块登录";或结束代码,如果用户尝试输入用户名或密码次数过多,但可以通过重新运行代码重试

import time 
usernames = ("test","test01", "test02")
passwords = ("test", "test01", "test02")
while True:
print("Username: ")
username = input("")
if username in usernames:
print("Username Accepted")
else:
print("Try Again (10 sec)")
time.sleep(10)
continue
break
while True:
print("Password: ")
password = input("")
if password in passwords:
print("Password Accepted")
else:
print("Try Again (10 sec)")
time.sleep(10)
continue
break
print("Access Granted")

我不知道你对"块登录";或";结束代码";但如果正确的密码被锁定,或者应该被锁定,这里有一种方法可以向调用代码发出信号:

import time 
usernames = ("test", "test01", "test02")
passwords = ("test", "test01", "test02")

def username():
print("Username: ")
username = input("")
if username in usernames:
print("Username Accepted")
else:
print("Try Again (10 sec)")
time.sleep(10)
continue
break
def password(n):
while True:
print("Password: ")
password = input("")
if password in passwords:
print("Password Accepted")
return True
n -= 1
if n == 0:
return False
print("Try Again (10 sec)")
time.sleep(10)  
username()
if not password(3):
print("Locked")
else:
print("Access Granted")

顺便说一句,最好不要透露用户名是否正确。换句话说,您读取(用户名、密码(,然后验证它,并告诉用户组合是否正确。此外,我会在第一次失败时睡眠0,但每次失败都会使情况呈指数级恶化:

带有验证器的用户名和密码

import time 
usernames = ("test","test01", "test02")
passwords = ("test", "test01", "test02")
max_attemp = 3
def validator(username, password):
isValid = False
if username in usernames and password in passwords:
if (list(usernames).index(username) == list(passwords).index(password)):
isValid = True
return isValid

attemps = 0
while True:
username = input("Your username:")
password = input("Your password:")
if validator(username, password):
print("Access Granted")
break
else:
attemps+=1
if attemps >= max_attemp:
print("Exceed max attemps.")
break

print("Try Again (10 sec)")
time.sleep(10)

您可以创建一个JSON文件来存储用户凭据以及他们的登录尝试。time_block键根据操作系统指定时间和日期,指示用户将被阻止的时间。

JSON文件:users.JSON

{
"users" : 
[
{
"username" : "test",
"password" : "test",
"blocked" : false,
"attempts" : 0,
"time_block" : ""
},
{
"username" : "test01",
"password" : "test01",
"blocked" : false,
"attempts" : 0,
"time_block" : ""
},
{
"username" : "test02",
"password" : "test02",
"blocked" : false,
"attempts" : 0,
"time_block" : ""
}

]
}

尽管我没有包括如果用户尝试次数过多,他将被锁定多长时间。如果你愿意,你可以试试。

import time 
import json
max_attempts = 5
user_data = json.load(open('users.json'))
current_user = None
while True:
print("Username: ")
username = input("")
is_user_found = [i[0] for i in enumerate(user_data["users"]) if i[1]["username"] == username]
if (is_user_found):
current_user = is_user_found
if user_data["users"][current_user[0]]["blocked"]:
print("User is locked")
continue
print("Username Accepted")
else:
print("Try Again (1 sec)")
time.sleep(1)
continue
break
while True:
user_data["users"][current_user[0]]["attempts"] += 1

print("Password: ")
password = input("")

if password == user_data["users"][current_user[0]]["password"]:
user_data["users"][current_user[0]]["attempts"] = 0
print("Password Accepted")
print("Access granted")
with open("users.json", "w") as output:
json.dump(user_data, output)

else:
if user_data["users"][current_user[0]]["attempts"] >= max_attempts:
user_data["users"][current_user[0]]["blocked"] = True
with open("users.json", "w") as output:
json.dump(user_data, output)

print("Account locked")
break
print("Try Again (1 sec)")
time.sleep(3)
continue
break

最新更新