当前位置:   article > 正文

进阶编程:编写登陆系统_软件开发 考试登入系统编写

软件开发 考试登入系统编写

(接上一篇文章:python 操作 mongo,MySQL,redis)

一.编写一个类

利用类来封装学生信息

class Student:
    def __init__(self,studentId,name,age,sex,class_id):
        self.studentId = studentId
        self.name = name
        self.age = age
        self.sex = sex
        self.class_id = class_id
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
if __name__ == '__main__':
    studentId = input('请输入学号:')
    name = input('请输入姓名:')
  • 1
  • 2
  • 3

二.再利用 python 操作 mysql

import pymysql
  • 1
db_config = {
    'host':'127.0.0.1',
    'port':3306,
    'user':'admin',
    'password':'qwe123',
    'db':'coco',
    'charset':'utf8'
}
conn = pymysql.connect(**db_config)
cur = conn.cursor()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
try:
     sql = f'SELECT * FROM `student` WHERE `id`={studentId} AND `name`="{name}";'
     cur.execute(sql)
     res = cur.fetchone()  # 获取查询到的单条数据
     if res:
         now_student = Student(*res)
     else:
         print('账号或密码错误,登陆失败')
         raise Exception	
 except Exception as e:
     conn.rollback()  # 如果执行sql语句出现了异常,则进行事务回滚
     print(f'异常{e},已取消操作')
 else:
     conn.commit()   # 确认没有问题,则进行事务提交
     print('执行成功')
 finally:
     # 无论是否成功执行完成之后都进行资源的释放
     cur.close()  # 关闭游标对象
     conn.close()   # 关闭连接对象

 print(f'当前登录的用户是:{now_student.name}')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

三.我们还可以设置验证码,就只要利用 python 控制 redis 即可使用验证码

import redis
import random
  • 1
  • 2
redis_config = {
    'host': '127.0.0.1',
    'port': 6379,
    'db': 0,
    'decode_responses':True
}
db = redis.StrictRedis(**redis_config)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
if __name__ == '__main__':
    studentId = input('请输入学号:')
    name = input('请输入姓名:')
    print('正在发送验证码。。。')
    code = str(random.randint(1000,9999))
    db.set(f'{studentId}:verification', code, ex=60)
    verifications = input(f'(验证码为:{code})')
    verification = input(f'请输入验证码:')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

(上文中的 student 是 redis 文章中已编写好的一个关于学生信息的数据库中的表格,random 模块是打印随机数。)
在这里插入图片描述

四.全部编程

import pymysql
import redis
import random

class Student:
    def __init__(self,studentId,name,age,sex,class_id):
        self.studentId = studentId
        self.name = name
        self.age = age
        self.sex = sex
        self.class_id = class_id


db_config = {
    'host':'127.0.0.1',
    'port':3306,
    'user':'admin',
    'password':'qwe123',
    'db':'coco',
    'charset':'utf8'
}
conn = pymysql.connect(**db_config)
cur = conn.cursor()

redis_config = {
    'host': '127.0.0.1',
    'port': 6379,
    'db': 0,
    'decode_responses':True
}
db = redis.StrictRedis(**redis_config)

if __name__ == '__main__':
    studentId = input('请输入学号:')
    name = input('请输入姓名:')
    print('正在发送验证码。。。')
    code = str(random.randint(1000,9999))
    db.set(f'{studentId}:verification', code, ex=60)
    verifications = input(f'(验证码为:{code})')
    verification = input(f'请输入验证码:')

    if verification != db.get(f'{studentId}:verification'):
        print('验证码错误 ')
        raise Exception

    now_student = None

    try:
        sql = f'SELECT * FROM `student` WHERE `id`={studentId} AND `name`="{name}";'
        cur.execute(sql)
        res = cur.fetchone()  # 获取查询到的单条数据
        if res:
            now_student = Student(*res)
        else:
            print('账号或密码错误,登陆失败')
            raise Exception


    except Exception as e:
        conn.rollback()  # 如果执行sql语句出现了异常,则进行事务回滚
        print(f'异常{e},已取消操作')
    else:
        conn.commit()   # 确认没有问题,则进行事务提交
        print('执行成功')
    finally:
        # 无论是否成功执行完成之后都进行资源的释放
        cur.close()  # 关闭游标对象
        conn.close()   # 关闭连接对象

    print(f'当前登录的用户是:{now_student.name}')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70

运行:
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/67539
推荐阅读
相关标签
  

闽ICP备14008679号