Python 如何使用外部文本文件创建身份验证

Python 如何使用外部文本文件创建身份验证,python,authentication,login,text-files,Python,Authentication,Login,Text Files,作为更大程序的一部分,我需要对照外部txt文件检查某人的用户名和密码 我需要在我的文本文件中使用字典还是什么 username = input("What is your username?") password = input("Password?") #To do: check username + password against external file users.txt #users.txt is in the same directory as the program 嗯,

作为更大程序的一部分,我需要对照外部txt文件检查某人的用户名和密码

我需要在我的文本文件中使用字典还是什么

username = input("What is your username?")
password = input("Password?")

#To do: check username + password against external file users.txt
#users.txt is in the same directory as the program


嗯,如果你的密码没有加密,你可以直接读取文件,然后把它分割成不同的用户和密码放在字典里

例如,如果文件的格式为每行一个用户和密码,并用空格分隔,则可以使用如下程序:

username = "username"
password = "password"

with open("users.txt", "r") as passwordsfile:
    passwordSeparator = " "
    # One line format, you can use one of the following solutions
    passwords = {
        x.split(passwordSeparator)[0]: x.split(passwordSeparator)[
            1
        ]  # x should be in "username password" format, so we have to split it
        for x in filter(
            None, passwordsfile.read().split("\n")
        )  # The filter function remove the empty lines
    }

    # Multiline solution:
    passwords = {}
    separatedLines = filter(None, passwordsfile.read().split("\n"))
    for x in separatedLines:
        _username = x.split(passwordSeparator)[0]
        _password = x.split(passwordSeparator)[1]
        passwords[_username] = _password


# print(passwords) : {"user1": "password1", "user2": "password2"}

# Check the user and password with assert
try:
    assert (
        passwords[username] == password
    )  # assert is useful to check a condition in our program. This is useful in a try/except bracket, because in this case, we have no guarentee that the username is in the password database
    print("You're logged in")
except:
    print("Wrong password or username!")

但是,如果您有一个大型用户数据库,您应该查看数据库存储(SQLite、MySQL等)的性能,并且您可能必须加密用户的密码。

如果您的密码没有加密,您可以直接读取该文件,并将其拆分为字典中的不同用户和密码

例如,如果文件的格式为每行一个用户和密码,并用空格分隔,则可以使用如下程序:

username = "username"
password = "password"

with open("users.txt", "r") as passwordsfile:
    passwordSeparator = " "
    # One line format, you can use one of the following solutions
    passwords = {
        x.split(passwordSeparator)[0]: x.split(passwordSeparator)[
            1
        ]  # x should be in "username password" format, so we have to split it
        for x in filter(
            None, passwordsfile.read().split("\n")
        )  # The filter function remove the empty lines
    }

    # Multiline solution:
    passwords = {}
    separatedLines = filter(None, passwordsfile.read().split("\n"))
    for x in separatedLines:
        _username = x.split(passwordSeparator)[0]
        _password = x.split(passwordSeparator)[1]
        passwords[_username] = _password


# print(passwords) : {"user1": "password1", "user2": "password2"}

# Check the user and password with assert
try:
    assert (
        passwords[username] == password
    )  # assert is useful to check a condition in our program. This is useful in a try/except bracket, because in this case, we have no guarentee that the username is in the password database
    print("You're logged in")
except:
    print("Wrong password or username!")

但是,如果您有一个大型用户数据库,您应该查看数据库存储(SQLite、MySQL等)的性能,并且您可能必须加密用户的密码。

从您的问题中我得到的任何信息都是您想要的 创建从文本文件中调用信息的登录/身份验证程序

def main():    
username, password = get_name_and_password()  
registered_users = read_pwdfile('pwd_filename')  
if usr_pass_registered(username, password, registered_users):  
    registered = True  
else:  
    registered = get_registration(username, password, 'pwd_filename')  
if registered:  
    print(You are logged in)  

#您需要以csv格式保存用户名和密码(逗号分隔)

我从您的问题中得到的信息是您想要 创建从文本文件中调用信息的登录/身份验证程序

def main():    
username, password = get_name_and_password()  
registered_users = read_pwdfile('pwd_filename')  
if usr_pass_registered(username, password, registered_users):  
    registered = True  
else:  
    registered = get_registration(username, password, 'pwd_filename')  
if registered:  
    print(You are logged in)  


#您需要将用户名和密码保存为csv格式(逗号分隔)

这些不是明文密码,是吗?这是一个学校项目,因此不需要加密。此外,所有的解算器-我仍在学习Python,所以请不要跳过太多步骤!这些不是明文密码,是吗?这是一个学校项目,所以不需要加密。另外,所有的解算器-我还在学习Python,所以请不要跳过太多步骤!尝试此操作后,出现以下错误代码:密码中的第14行={x.split(“”[0]:x.split(“”[1]表示筛选器中的x(无,passwordsfile.read().split(“\n”)}文件“filename”,密码中的第14行={x.split(“”[0]:x.split(“”[1]表示筛选器中的x(无,passwordsfile.read().split(“\n”))}索引器错误:列表索引超出范围抱歉,如果格式太差,请先使用post.username密码。它应该是逗号分隔的还是什么?好吧,这应该适用于空格,但您的错误表明一行的格式不正确,因为x变量无法拆分。(我更新了我的代码,使之更清晰)。完成!非常感谢,这将非常有用。尝试此操作后,出现以下错误代码:第14行,在密码中={x.split(“”[0]:x.split(“”[1]表示过滤器中的x(无,passwordsfile.read().split(“\n”)}文件名,第14行,在密码中={x.split(“”[0]:x.split(“”[1]),表示过滤器中的x(无,passwordsfile.read().split(“\n”)}索引器错误:列表索引超出范围抱歉,如果格式太差,请先使用post.username密码。它应该是逗号分隔的还是什么?好吧,这应该适用于空格,但您的错误表明一行的格式不正确,因为x变量无法拆分。(我更新了我的代码,使之更清晰)。完成!非常感谢,这将非常有帮助。“非类型对象不可iterable。”“非类型对象不可iterable。”