繁体   English   中英

Python登录脚本; 用户名和密码在一个单独的文件中

[英]Python Login Script; Usernames and Passwords in a separate file

我正在寻求帮助,让我的 Python 脚本模仿登录功能,同时将凭据存储在单独的文件中。

我通过硬编码的用户名和密码让它工作,它也读入一个文件,但我很难找到如何将两者链接在一起。

任何帮助表示赞赏。

Python脚本如下:

print "Login Script"

import getpass

CorrectUsername = "Test"
CorrectPassword = "TestPW" 

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

    username = raw_input("Please enter your username: ")

    if (username == CorrectUsername):
        loop1 = 'true'
        while (loop1 == 'true'):
            password = getpass.getpass("Please enter your password: ")
            if (password == CorrectPassword):
                print "Logged in successfully as " + username
                loop = 'false'
                loop1 = 'false'
            else:
                print "Password incorrect!"

    else:
        print "Username incorrect!"

我在其他地方找到了这个帮助我读入文件的东西,它确实打印了文本文件的内容,但我不确定如何从这里取得进展:

with open('Usernames.txt', 'r') as f:
    data = f.readlines()
    #print data

for line in data:
    words = line.split() 

该文本文件包含格式为:Test:TestPW Chris:ChrisPW Admin:AdminPW 的用户名和密码,每个凭据都在一个新行上。

正如我之前所说,任何帮助表示赞赏! 谢谢。

你可以开始拥有一个用户名和密码字典:

credentials = {}
with open('Usernames.txt', 'r') as f:
    for line in f:
        user, pwd = line.strip().split(':')
        credentials[user] = pwd

然后你有两个简单的测试:

username in credentials

会告诉你用户名是否在凭证文件中(即如果它是credentials字典中的一个键)

进而:

credentials[username] == password
import hashlib ,os
resource_file = "passwords.txt"
def encode(username,password):
    return "$%s::%s$"%(username,hashlib.sha1(password).hexdigest())
def add_user(username,password):
    if os.path.exists(resource_file):
        with open(resource_file) as f:
            if "$%s::"%username in f.read():
                raise Exception("user already exists")
    with open(resource_file,"w") as f:
         print >> f, encode(username,password)
    return username
def check_login(username,password):
    with open(resource_file) as f:
        if encode(username,password) in f.read():
           return username

def create_username():
     try: 
         username = add_user(raw_input("enter username:"),raw_input("enter password:"))
         print "Added User! %s"%username
     except Exception as e:
         print "Failed to add user %s! ... user already exists??"%username
def login():
     if check_login(raw_input("enter username:"),raw_input("enter password:")):
        print "Login Success!!"
     else:
        print "there was a problem logging in"

while True:
    {'c':create_username,'l':login}.get(raw_input("(c)reate user\n(l)ogin\n------------\n>").lower(),login)()

您不应该使用 2 个循环。 它只会告诉人们他们猜到了用户名。 就是说。 使用一个循环。

还要检查我的 repl.it it 页面以获得更好的 sso,它可以同时容纳 100 人,而没有 else if 语句

这是: https : //repl.it/@AmazingPurplez/CatSSO

有错误。 仅由我开发,所以 +rep 给我。

  • 代表:乔兰·比斯利

由于“缩进错误”而无法在此处发布代码,例如 frick it! 但我还是会尝试更简单的版本

import getpass
username = "username"
password = "password"
loop = True
while loop == True:
    userinput = input("question")
    passinput = getpass.getpass("question")
    if userinput == username and passinput == password:
        statements
        break
    else:
        statements
username = raw_input("Username:")
password = raw_input("Password:")
if password == "CHANGE" and username == "CHANGE":
    print "Logged in as CHANGE"

else:
    print "Incorrect Password. Please try again."

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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