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

Block login if too many login attempts

如果用户多次尝试输入用户名或密码,我想“阻止登录”或结束代码,但可以通过重新运行 代码

重试
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")

顺便说一句,最好不要透露用户名是否正确。换句话说,您读取 (username, password) 然后验证它并告诉用户组合是否正确。另外,我会在第一次失败时睡 0,然后在每次失败时使它 exponentially 变得更糟:

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

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 键根据您的 OS 指定时间和日期,这表明该用户将被阻止多长时间。

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