我的 Python 登录脚本有问题.尝试的语法无效



我在尝试编写的当前程序时遇到问题。我不明白为什么它一直这么说或为什么。

此外,是否可以扩展此代码以涵盖IP日志记录并确保理论上可以登录多个用户/同一IP?

这是代码:

import hashlib
import time
#cPickle is faster then pickle but not available in all python releases
#thats why i used a try/accept there
try: import cPickle as cp

#load the database if it exist, if not it create one
try:
    f =(r"C:UsersOwnerDesktoppythondatabase.data")
    data = cp.load(f)
except IOError:
    data = {}
#A simple function made to make data dumping easy
def easyDump(data_):
    f = file(r"C:UsersOwnerDesktoppythondatabase.data", "w")
    cp.dump(data_, f)
    f.close()
#Get's the date (We'll use this as the custom salt)
def getData():
    return str(time.strftime("%d-%m-%Y"))
#A function which accepts two parameters, password and date.
#The date is the custom salt. It returns the sha512 hash excetpyion
def  salt(password, date):
    salted = hasglib.sha512(password + str(data)).hexdigest()
    retun str(salted)
menu = """"
1.Login
2.Register
3.Exit
"""
while True:
    print menu
    choice = int(raw_input("Your choice please: "))
    if choice ==1:
        username = raw_input("Enter your username please: ")
        password = raw.input("Enter your authentication code please: ")
        #if the username is found on the database
        if data.has_key(username):
            #date is equal to our secured stored data
            date = date[username][1]
            #check of the given password  + date is equal to what is stored on the database
            #password
            if salt(password, date) == date[username][0]:
                print"Welcome %s!" % username
            else:
                print "Incorrect password"
            else:
                print "user %s not found, please register!" % username
        elif choice == 2:
            username = raw_input("Please enter yout username: !")
            password = raw_input("Please enter your password: !")
            #if username exists in the system already then the name is taken
            if data.has_key(username):
                print "user %s already registered, please put in another % username
            else:
                #in order words data = {username: hash, date}
                data[username] = [salt(password, getData()), get Data()]
                easyDump(data)
                print "user %s successfully registereed!" %username
            elif choice == 3:
                print "goodbye!"
                break
            else:
                print "invaid input or commands"

此代码:

try: import cPickle as cp

后面没有except...因此语法错误

您的代码包含许多缩进、语法错误和拼写错误。以下修复了这些内容以允许它至少运行:

import hashlib
import time
#cPickle is faster then pickle but not available in all Python releases
#That is why I used a try/accept there
try: 
    import cPickle as cp
except:
    import pickle as cp
#load the database if it exist, if not it create one
try:
    f = open(r"C:UsersOwnerDesktoppythondatabase.data")
    data = cp.load(f)
except IOError:
    data = {}
#A simple function made to make data dumping easy
def easyDump(data_):
    f = file(r"C:UsersOwnerDesktoppythondatabase.data", "w")
    cp.dump(data_, f)
    f.close()
#Get's the date (We'll use this as the custom salt)
def getData():
    return str(time.strftime("%d-%m-%Y"))
#A function which accepts two parameters, password and date.
#The date is the custom salt. It returns the sha512 hash exception
def salt(password, date):
    salted = hasglib.sha512(password + str(data)).hexdigest()
    return str(salted)      
menu = """"
1.Login
2.Register
3.Exit
"""
while True:
    print menu
    choice = int(raw_input("Your choice please: "))
    if choice == 1:
        username = raw_input("Enter your username please: ")
        password = raw.input("Enter your authentication code please: ")
        #if the username is found on the database
        if data.has_key(username):
            #date is equal to our secured stored data
            date = date[username][1]
            #check of the given password  + date is equal to what is stored on the database
            #password
            if salt(password, date) == date[username][0]:
                print"Welcome %s!" % username
            else:
                print "Incorrect password"
        else:
            print "user %s not found, please register!" % username
    elif choice == 2:
        username = raw_input("Please enter yout username: !")
        password = raw_input("Please enter your password: !")
        #if username exists in the system already then the name is taken
        if data.has_key(username):
            print "user %s already registered, please put in another" % username
        else:
            #in order words data = {username: hash, date}
            data[username] = [salt(password, getData()), getData()]
            easyDump(data)
            print "user %s successfully registered!" % username
    elif choice == 3:
        print "goodbye!"
        break
    else:
        print "invalid input or commands"

在 Python 中缩进非常重要,出错会完全改变代码的含义。

最新更新