Py学习  »  Python

希望你能得到附加python程序的建议。我们可以使用字典作为数据库,用于非常基本的登录系统程序吗?

Srinivas • 3 年前 • 194 次点击  

我正在尝试创建登录系统程序,其中“姓名”、“电子邮件”和“密码”必须从用户处作为输入,并保存为文本文件中的字典。我被困在如何给每个命题分配一个变量上。 此外,一旦用户注册后再次出现,应该对字典文本文件进行密码检查,我不确定。请查看下面的代码并提供帮助?

#! usr/bin/python3
# Login project sample
import json
def login():

    print("Welcome to login system")
    New_user = input("Are you a new user? type yes or no: ")
    if New_user == "yes":
        a = {}
        Name = input("Enter your name: ")
        Email = input("Enter the email: ")
        Password = input("Enter the password: ")
        a["Name"] = (Name)
        a["Email"] = (Email)
        a["Password"] = (Password)
        print(a)
        with open("login.txt", "a+") as f:
            f.seek(0)
            data = f.read(100)
            if len(data) > 0:
                f.write("\n")
                x = str(range(1,100))
                f.write(x + "=" + json.dumps(a))
                x = x + 1
    else:
        b={}
        def login1():

            Email = input("Enter the email: ")
            Password = input("Enter the password: ")
            b["Email"] = (Email)
            b["Password"] = (Password)
            with open("login.txt", "r") as r:
                f.seek(0)
                i = range(100)
                if i.keys() in b.keys() and i.value() in b.value():
                    print("Login is successfull")
                else:
                    login1()
login()
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/133784
 
194 次点击  
文章 [ 1 ]  |  最新文章 3 年前
Pawel Kam
Reply   •   1 楼
Pawel Kam    3 年前

我建议不要1)使用字典作为持久存储身份验证数据的方式,2)将用户密码直接存储在这样的数据库中。

如果你真的不需要使用字典,那么一些简单的解决方案 sqlite (.db文件而不是.txt文件)和一些散列库可以帮助您实现目标。代码可能是这样的:

import sqlite3
import hashlib


def create_table():
    query = "DROP TABLE IF EXISTS login"
    cursor.execute(query)
    conn.commit()

    query = "CREATE TABLE login(name VARCHAR DEFAULT '', email VARCHAR NOT NULL UNIQUE, password VARCHAR NOT NULL)"
    cursor.execute(query)
    conn.commit()

def add_user(name, email, raw_password):
    query = "INSERT INTO login (name, email, password) VALUES (?, ?, ?)"
    hashed_password = hashlib.sha256(raw_password.encode('utf-8')).hexdigest()
    cursor.execute(query, (name, email, hashed_password))
    conn.commit()

def check_user(name, email, raw_password):
    query = 'SELECT * FROM login WHERE name = ? AND email = ? AND password = ?'
    hashed_password = hashlib.sha256(raw_password.encode('utf-8')).hexdigest()
    cursor.execute(query, (name, email, hashed_password))
    result = cursor.fetchone()
    conn.commit()
    print('[DEBUG][check] result:', result)
    return result

def login():
    answer = input("Login (Y/N): ")
    if answer.lower() == "y":
        name = input("Name: ")
        email = input("Email: ")
        password = input("Password: ")
        if check_user(name, email, password):
            print("Email correct!")
            print("Password correct!")
            print("Logging in...")
        else:
            print("Something wrong")

# --- main ---

conn = sqlite3.connect("users.db")
cursor = conn.cursor()

create_table()  # use only once

name = input("New name: ")
email = input("New email: ")
password = input("New password: ")

add_user(name, email, password)

login()

cursor.close()
conn.close()