Discuss / Python / 实现作业二,将用户db保存至本地文件。

实现作业二,将用户db保存至本地文件。

Topic source
import hashlib,json

def get_md5(raw_password):
    md5 = hashlib.md5()
    md5.update(raw_password.encode('utf-8'))
    return md5.hexdigest()

def register(username, password):
    print ("----registration----")
    db[username] = get_md5(password + username + 'the-Salt')   #put something in when cal the password md5 value
    print ("registration complete! password=%s" %str(db[username]))
    with open('user_db.txt', 'w') as f:                            #save new user profile
        f.write(str(db))

def login(username, password):

    print ("----login----")
    pass_md5 = get_md5(password + username + 'the-Salt')      #put something in when cal the md5 value
    try:
        if pass_md5 == db[username]:
            return True;
        else:
            return False;
    except:
        return "user not found!"


#main start
with open('user_db.txt', 'r') as f:       #open user data file
    json_str = f.read()
str_new = ''
for i in json_str:                          #convert the normal string format to json string format 
    if i == "'":
        str_new = str_new + "\""
    else:
        str_new = str_new + i

try:
    db = json.loads(str_new) 
except:
    db = {}                               #initialize db if user_db.txt is empty 

choose = input("select a option: \n 1.new user registration\n 2.login\n")  #options menu
user = input("input username please: ")
passwd = input("input password please: ")
if choose == "1":
    register(user,passwd)
elif choose == "2":
    result = (login(user,passwd))
    if result == True:
        print ("login successfully!")
    elif not result:
        print ("login failed, password wrong!")
    else:
        print (result)
else:
    print ("option num not found!")
print ("finish")

第一次运行:创建用户1 select a option: 1.new user register 2.login 1 input username please: user1 input password please: password1 ----registration---- registration complete! password=0664696dc7697f745db03930672c7375 finish

Repl Closed

第二次运行:创建用户2 select a option: 1.new user register 2.login 1 input username please: user2 input password please: password2 ----registration---- registration complete! password=960cdeff4e01c1a3e1232652c4ada845 finish

Repl Closed

第三次运行:正确login示例 select a option: 1.new user register 2.login 2 input username please: user1 input password please: password1 ----login---- login successfully! finish

Repl Closed

第四次运行:错误密码示例 select a option: 1.new user register 2.login 2 input username please: user1 input password please: password1wrong ----login---- login failed, password wrong! finish

Repl Closed

第五次运行:错误用户示例 select a option: 1.new user registration 2.login 2 input username please: userbigboss input password please: asdasd ----login---- user not found! finish

Repl Closed

'user_db.txt'文件内容:

{'user1': '0664696dc7697f745db03930672c7375', 'user2': '960cdeff4e01c1a3e1232652c4ada845'}

玉山-A

#2 Created at ... [Delete] [Delete and Lock User]

我运行你的代码,注册成功但是每次登陆都说"user not found",而且我发现我注册了好几个但是,txt文件里永远都是最后注册的账号,能告诉我是怎么会事儿嘛


  • 1

Reply