Domanda

I've asked a question similar and can be found here: Python Login Script; Usernames and Passwords in a separate file

However, I'm now looking to extend the script so that it can look at multiple passwords per user account in the text file and know that the first password is the legitimate password while any other specified password linked to that account will throw up an error message.

So right now the external username file is set up so that its:

Admin:AdminPW

Now I'm looking to extend it to be set up something similar like

Admin:AdminPW;adminpw;adminpw123;Admin

where the last 4 passwords, if entered, would throw up a specific error message

My code is:

import getpass

credentials = {}                                                            ## Sets up an array for the login credentials
with open('Usernames.txt', 'r') as f:                                       ## Opens the file and reads it
    for line in f:  ## For each line
        username, password = line.strip().split(':')                        ## Separate each line into username and password, splitting after a colon
        credentials[username] = password                                    ## Links username to password

loop = 'true'
while (loop == 'true'):

    username = raw_input("Please enter your username: ")                    ## Asks for username

    if (username in credentials):                                           ## If the username is in the credentials array
        loop1 = 'true'
        while (loop1 == 'true'):
            password = getpass.getpass("Please enter your password: ")      ## Asks for password
            if (password == credentials[username]):                         ## If the password is linked to the username
                print "Logged in successfully as " + username               ## Log in
                loop = 'false'
                loop1 = 'false'
            else:
                print "Password incorrect!"

    else:
        print "Username incorrect!"

I've tried using the ".strip(';')" but it didn't work. I'm still relatively inexperienced with Python so I'm unsure what to do next

Any help is greatly appreciated!

È stato utile?

Soluzione

import getpass

credentials = {}                                                            ## Sets up an array for the login credentials
with open('Usernames.txt', 'r') as f:                                       ## Opens the file and reads it
    for line in f:  ## For each line
        username, delim, password = line.strip().partition(':')                        ## Separate each line into username and password, splitting after a colon
        credentials[username] = password.split(';')                                    ## Links username to password

while True:
    username = raw_input("Please enter your username: ")                    ## Asks for username
    if username in credentials:                                           ## If the username is in the credentials array
        while True:
            password = getpass.getpass("Please enter your password: ")      ## Asks for password
            if password == credentials[username][0]:
                print "Logged in successfully as " + username               ## Log in
                break
            elif password in credentials[username]:                         ## If the password is linked to the username
                print "Specific error message " + username               ## Log in
            else:
                print "Password incorrect!"
        break
    else:
        print "Username incorrect!"

It's simpler though if you just ask for a username/password fresh each time. The way you have it - if the user enters someone else's username by mistake they are stuck in a loop forever unless they can guess the password.

while True:
    username = raw_input("Please enter your username: ")                     
    password = getpass.getpass("Please enter your password: ")      
    if username in credentials and password == credentials[username][0]:
        print "Logged in successfully as " + username    
        break
    elif password in credentials.get(username, []):
        print "Specific error message " + username    
    else:
        print "Login incorrect!"

Altri suggerimenti

I have a password program that I have done as part of my GCSE coursework. It is python 3.2.3 but the principle is the same. You want to encrypt your passwords. The best way to do this (i think) is hashing the password and saving it in a .txt file. example...

file = open("pwdfile.txt", "a+")
pwd = getpass.getpass("Enter Password...").encode("utf-8")
pwd = hashlib.sha512(pwd).hexdigest()
file.write(pwd)
file.close()

Now your password is completely encrypted, no cracker would ever know what it is. pwdfile.txt looks like this: "522e8eca613e7b41251ad995ba6406571f0b62a701e029c8e1eb24cb3b93f89a95c296aa91cde7dcb8da86fda66eda5432b206a7bc3e9b74f033d961da962e1b"

Now to read the password, ie. to login, you take the users password input, hash it in the same way and if the two match, log them in. The advantage to hashing is that even if a hacker gets hold of the pwdfile.txt they can't "decode" it as such.

To log someone in:

file = open("pwdfile.txt", "a+")
pwd = file.read()
userpwd = getpass.getpass("Enter password...").encode("utf-8")
userpwd = hashlib.sha512(pwd).hexdigest()
if userpwd == pwd:
   print("LOGIN")
else:
   print("ERROR")

Hope this helps! :D

password == credentials[username] I did not understand what you are trying to do with this line. credentials[username] will return all passwords as a single string. If you are trying to look for a match with the password entered and the values you are storing, then split again with split(;) and compare with each result.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top