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

时间:2014-02-04 18:42:34

标签: python login scripting

我正在寻求帮助以使我的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() 

文本文件包含以下格式的用户名和密码:测试:TestPW Chris:ChrisPW管理员:AdminPW,每个凭据都在新行上。

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

4 个答案:

答案 0 :(得分:2)

您可以开始使用用户名和密码字典:

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

答案 1 :(得分:2)

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 :(得分:0)

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

还要检查我的repl.it页面,以获得更好的sso,使其一次可以有100个人,而无需if语句

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

有错误。仅由我开发,因此+ rep给我。

  • 回复:Joran Beasley

由于出现“缩进错误”,因此无法在此处发布代码!但我仍然会尝试一个更简单的版本

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

答案 3 :(得分:-1)

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