Python登录脚本;每个帐户多个密码 [英] Python Login Script; multiple passwords per account

查看:199
本文介绍了Python登录脚本;每个帐户多个密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问过类似的问题,可以在这里找到:

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

Admin:AdminPW

现在我想扩展它以设置类似的东西

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

Admin:AdminPW; adminpw; adminpw123; Admin

Admin:AdminPW;adminpw;adminpw123;Admin

如果输入了最后4个密码,则会抛出特定的错误消息

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

我的代码是:

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!"

我尝试使用".strip(';')",但是没有用.我对Python还是比较缺乏经验,所以我不确定下一步该怎么做

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

任何帮助将不胜感激!

推荐答案

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!"

这篇关于Python登录脚本;每个帐户多个密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆