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

问题描述 投票:1回答:4

我在寻找帮助让我的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克里斯:ChrisPW管理:与上一个新行每个凭据AdminPW。

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

python login scripting
4个回答
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

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)()

0
投票

你不应该使用2个循环。它只是告诉他们猜对了用户名的人。只是说。使用一个循环。

还检查我的repl.it它页SSO这更好的能有像100人一下子没有else if语句

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

有错误。只有通过我这么+代表对我的发展。

  • 代表到:Joran比斯利

无法在这里发表,因为“缩进错误”之类弗里克它的代码!但我还是会尽量简化版本

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

-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."
© www.soinside.com 2019 - 2024. All rights reserved.