Python登录程序

时间:2015-02-07 14:42:24

标签: python security login passwords

我正在用Python编写一个登录程序,它接受用户名和密码,并将它存储在一个单独的.txt文件中。 我已经可以注册,程序创建文件,但我无法登录。 这是代码:

###############
import getpass
import time
ussr=False
passwd1=False
acc=False
notin=False
reg=False
t=[]
###############
#f;a;;b;c;d;z;;;;;
###############
def bor():
    global acc
    print("1-->Login")
    print("2-->Register")
    a=int(input("B/R: "))
    if  a==1:
        acc=True
    if a==2:
        acc=False

def ACCOUNT():
    if acc==True:
        login()
    if acc==False:
        register()

def file():
    global jel
    f=open(z,"r")
    sor=f.read()
    jel=sor.strip().split()
    for s in jel:
        t.append(s)
    f.close()


def register():
    global reg
    reg=True
    global z
    b=input("Username: ")
    c=getpass.getpass('Passwd')
    z=input("Filename(.txt!):")
    f2=open(z,"w")
    f2.close()
    f=open(z,"r+")
    if b not in f:
        notin=True
    f.close
    if notin==True:
        f1=open(z,"a")
        f1.write(b)
        f1.write(c)
        f1.close
    if notin==False:
        print("This username is already taken")
        exit

def login():
    global usr
    global passwd
    global passwd1
    global ussr
    global z
    usr=input("Username: ")
    passwd=getpass.getpass('Password')
    z=input("Filename(.txt!):")
    for i in t:
        if usr==i:
            ussr=True
        if passwd==i:
            passwd1=True

def check():
    if reg==True:
        exit
    if ussr==True and passwd1==True:
        print("Login succesful")
        time.sleep(12)
    if ussr==True and passwd1==False:
        print("Wrong password")
    if ussr==False and passwd1==True:
        print("Wrong username")





bor()
ACCOUNT()
file()
check()

1 个答案:

答案 0 :(得分:1)

请避免使用全局变量,并开始编程更多“pythonic”。开始使用类对象和函数参数。 实际上,全局变量是坏的。

然后:

def ACCOUNT():
    if acc==True:
        login()
    if acc==False:
        register()

是一个可怕的陈述。

def ACCOUNT(acc):
   if acc: login() 
   else: register()

更好。

当你写条件时,你必须考虑“if,else,elif”:

if a == 1: 
   doSomething()
elif a == 2:
   doSomethingElse()
else:
   doAnotherThing()

要清楚,除非你需要检查变量类型,通常“if varname”是正确的(如果对象为空,它将返回false,或者如果布尔值等于False和None)。

如果必须检查布尔值,通常不必指定要查找的条件。

“if not a”更多pythonic 而不是“if a == False”(但同样,它也将匹配“None”而不仅仅是“False”!!)

告诉自己,每当你宣布一个全局变量时:“我做错了什么,而且肯定有更好的方法来做到这一点

您可以将变量作为函数参数传递:

def sumFunction(arg1, arg2):
    return arg1 + arg2

你可以声明类,存储变量,处理同一任务的函数,并开始编程更好:

class Authentication():
    def __init__(self):
        # the __init__ class method is run everytime the function is istantiated
        self.example = 'I am an example'

    def Login(self, username, password):
        # this will do authentication things with
        # username and password variables, that lives
        # ONLY in this function (namespace)

    def Register(self, username, password):
        # do register things

然后你可以像这样

来实现类认证
auth = Authentication()

并根据需要使用实例:

auth.Login(username, password)
auth.Register(username, password)

“self”是类名称空间,从它自身查看。 你可以在里面存储你喜欢的任何内容,你可以在课堂上调用子功能,在“自我”之前。前缀

在我提供的示例中,您可以通过以下方式访问“我是示例文本”:

>>> x = Authentication()
>>> x.example
'I am an example'

最后,当你声明一个类方法(类中的一个函数)时,你总是必须在第一个参数中指定“self”,但是从外部调用方法时你不需要传递它(所以a带有3个参数的类方法:(self,arg1,arg2),可以使用两个参数,只需要arg1和arg2。

阅读更多,少写,祝你好运。