zoukankan      html  css  js  c++  java
  • 每日作业5/7

    """
    1 navicat自己玩一玩
    2 练习题一定要搞懂 照着我的思路一遍遍的看敲
    3 熟悉pymysql的使用
    4 sql注入产生的原因和解决方法 了解
    5 思考:如何结合mysql实现用户的注册和登录功能?
    """

    登录注册:

    #数据库服务器地址
    MYSQL_HOST="localhost"
    MYSQL_PORT=3306
    MYSQL_USER="root"
    MYSQL_PASSWD="123456"
    MYSQL_DATABASE="atm_db"
    MYSQL_CHARSET="UTF8"
    settings.py
    from interface import user_interface
    from lib import common
    
    login_user = None
    
    
    # 用户注册
    def register():
        while True:
            name = input("请输入注册的用户名:>>>").strip()
            if name == "b": break
            passwd = input("请输入密码:>>>").strip()
            re_passwd = input("请确认密码:>>>").strip()
            if passwd == re_passwd:
                passwd_md5 = common.get_md5(passwd)
                flag, msg = user_interface.back_register(name, passwd_md5)
                if flag:
                    print(msg)
                    break
                else:
                    print(msg)
            else:
                print("两次密码输入不一致,请重新输入")
    
    
    # 用户登录
    def login():
        global login_user
        while True:
            name = input("请输入用户名:>>>").strip()
            if name == "b": break
            passwd = input("请输入密码:>>>").strip()
            passwd_md5 = common.get_md5(passwd)
            flag, msg = user_interface.back_login(name, passwd_md5)
            if flag:
                print(msg)
                login_user = name
                break
            else:
                print(msg)
    
    
    # 查看余额
    @common.auth
    def check_balance():
        # print(login_user)
        flag,result=user_interface.back_balance(login_user)
        if flag:
            print("用户{}余额为【{}】元".format(login_user,result["balance"]))
    
    
    func = {
        "0": exit,
        "1": register,
        "2": login,
        "3": check_balance,
    }
    
    
    # atm主界面
    def run():
        while True:
            print("欢迎来到ATM主界面".center(20, "="), end="")
            print("""
            1.注册
            2.登录
            3.查询余额
            """)
            choice = input("请输入功能编号:>>>").strip()
            if choice in func:
                func.get(choice)()
            else:
                print("输入的功能编号不存在")
    
    if __name__ == '__main__':
        res, result = user_interface.back_balance("egon")
        print("用户{}余额为【{}】".format("egon", result["balance"]))
    src.py
    from conf import settings
    import pymysql
    
    
    # 封装数据库连接代码
    class MysqlHandle:
        def __init__(self):
            self.host = settings.MYSQL_HOST
            self.port = settings.MYSQL_PORT
            self.user = settings.MYSQL_USER
            self.password = settings.MYSQL_PASSWD
            self.database = settings.MYSQL_DATABASE
            self.charset = settings.MYSQL_CHARSET
            self.conn = pymysql.connect(host=self.host, port=self.port, user=self.user,
                                   password=self.password,
                                   database=self.database, charset=self.charset)
            self.cursor = self.conn.cursor(pymysql.cursors.DictCursor)  # 执行完毕返回的结果集默认以字典显示
    
    
        def load_info(self, sql,val):
            res = self.cursor.execute(sql,val)
            result = self.cursor.fetchall()
            return res, result
    
        def insert_data(self,sql,val):
            res = self.cursor.execute(sql,val)
            self.conn.commit()
            return res
    
        def close_mysql(self):
            self.cursor.close()
            self.conn.close()
    db_handle.py
    import pymysql
    from db import db_handle
    
    
    # 用户注册
    def back_register(username, passwd):
        # 数据库连接
        mysql_obj = db_handle.MysqlHandle()
        # 查看用户是否已经存在
        sql = 'select * from userinfo where username=%s '
        val = (username)
        res, result = mysql_obj.load_info(sql, val)
        # print(res)
        if res:
            mysql_obj.close_mysql()
            return False, "用户已存在"
        else:
            # sql = 'insert into userinfo(username,password) values(%s,%s);'
            # res=cursor.execute(sql,(username,passwd_md5))
            sql1 = 'insert into userinfo(username,password) values(%s,%s);'
            val1 = (username, passwd)
            res1 = mysql_obj.insert_data(sql1, val1)
            if res1:
                mysql_obj.close_mysql()
                return True, "注册成功"
    
    
    # 用户登录
    def back_login(username, passwd):
        # 数据库连接
        mysql_obj = db_handle.MysqlHandle()
        # 查看用户是否存在
        sql = 'select * from userinfo where username=%s '
        val = (username)
        res, result = mysql_obj.load_info(sql, val)
        if not res:
            mysql_obj.close_mysql()
            return False, "用户不存在"
    
        sql1 = 'select * from userinfo where username=%s and password=%s '
        val1 = (username, passwd)
        res1, result1 = mysql_obj.load_info(sql1, val1)
        if not res1:
            mysql_obj.close_mysql()
            return False, "密码错误"
        mysql_obj.close_mysql()
        return True, "登录成功"
    
    
    # 查看余额接口
    def back_balance(username):
        # 数据库连接
        mysql_obj = db_handle.MysqlHandle()
        sql = 'select * from userinfo where username=%s '
        val = (username)
        res, result = mysql_obj.load_info(sql, val)
        if res:
            return True, result[0]
    user_interface.py
    import hashlib
    
    
    #获取md5值
    def get_md5(password):
        salt="I love python"
        m=hashlib.md5()
        m.update(salt.encode("utf-8"))
        m.update(password.encode("utf-8"))
        return m.hexdigest()
    
    #登录验证
    def auth(func):
        from core import src
        def inner(*args,**kwargs):
            if src.login_user:
                res=func(*args,**kwargs)
                return res
            else:
                print("请先登录")
                src.login()
                res = func(*args, **kwargs)
                return res
        return inner
    
    
    if __name__ == '__main__':
        res=get_md5("123456")
        print(res)
    common.py
    from core import src
    
    if __name__ == '__main__':
        src.run()
    start.py
  • 相关阅读:
    springmvc介绍
    mybatis中的动态sql应用
    mybatis中表的关联
    mybatis分页
    聚类评估指标系列(二):准确率和F值
    混淆矩阵,准确率,召回率,F-score,PR曲线,ROC曲线,AUC
    聚类评估指标系列(一):标准化互信息NMI计算步骤及其Python实现
    numpy.where() 用法详解
    互信息Mutual Information
    转:Prewitt算子、Sobel算子、canny算子、Lapacian算子
  • 原文地址:https://www.cnblogs.com/baicai37/p/12844807.html
Copyright © 2011-2022 走看看