简体   繁体   中英

Python login verification system

This is a login system where the user types in their details and then this code verifies if the user exists

counter = 0
def validation():
    global counter
    print("Sorry, this username or password does not exist please try again")
    counter += 1
    if counter == 3:
        print("----------------------------------------------------")
        print("You have been locked out please restart to try again")
        sys.exit()

def verify_login(username, password, login_data):
    for line in login_data:
       if ("username: " + username + " password: " + password) == line.strip():
       return True

    return False


check_failed = True
while check_failed:
    with open("accountfile.txt","r") as username_finder:
        print("Could player 1 enter their username and password")
        username1=input("Please enter your username ")
        password1=input("Please enter your password ")
        if verify_login(username1, password1, username_finder):
            print("you are logged in")

Right now the code looks in the text file for this format:

username: (username) password: (password)

But now I want to add a hash code for every user so the format in the file looks like this:

49ad2322f9a401e9e3c4ae7694b6b1f1 username: (username) password: (password)

How would I change the verfiy_login to make it look for a random 32 characters at the beginning of every user?

Instead of reconstructing the line using "username: " + username + " password: " + password then comparing it to the line from the text file, you can parse it by first split ting the string then unpacking the tokens. By splitting the line, you get access to the hash, username, and password individually as variables.

def verify_login(username, password, login_data):
    for line in login_data:

       hsh, _, username_from_file, _, password_from_file = line.strip().split()
       if len(hsh) == 32 and username == username_from_file and password == password_from_file:
            return True

    return False

Unpacking a token into _ is a convention saying that "we will dump this value".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM