一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術(shù)及教程分享平臺!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - Python實現(xiàn)的登錄驗證系統(tǒng)完整案例【基于搭建的MVC框架】

Python實現(xiàn)的登錄驗證系統(tǒng)完整案例【基于搭建的MVC框架】

2021-06-15 00:52微信1257309054 Python

這篇文章主要介紹了Python實現(xiàn)的登錄驗證系統(tǒng),結(jié)合完整實例形式分析了Python基于搭建的MVC框架進(jìn)行登錄驗證操作的相關(guān)實現(xiàn)與使用技巧,需要的朋友可以參考下

本文實例講述了python實現(xiàn)的登錄驗證系統(tǒng)。分享給大家供大家參考,具體如下:

小型登錄注冊驗證系統(tǒng)

一、概述

? 使用redis+mysql數(shù)據(jù)庫實現(xiàn)一個小型的登錄注冊驗證系統(tǒng)。在這個系統(tǒng)中初步了解認(rèn)識mvc框架。

? 具備功能:登錄、注冊、改密、注銷。

? 數(shù)據(jù)庫:redis,mysql。使用redis把用戶信息存儲在內(nèi)存中,查詢數(shù)據(jù)快。mysql存儲空間更大,對表之間的關(guān)系管理更好。兩者結(jié)合使用發(fā)揮各自的優(yōu)勢已是當(dāng)下流行的數(shù)據(jù)庫使用方式。

? 開發(fā)語言:python。

? mvc框架:mvc全名是model view controller,是模型(model)-視圖(view)-控制器(controller)的縮寫,一種軟件設(shè)計典范,用一種業(yè)務(wù)邏輯、數(shù)據(jù)、界面顯示分離的方法組織代碼,將業(yè)務(wù)邏輯聚集到一個部件里面,在改進(jìn)和個性化定制界面及用戶交互的同時,不需要重新編寫業(yè)務(wù)邏輯。mvc被獨特的發(fā)展起來用于映射傳統(tǒng)的輸入、處理和輸出功能在一個邏輯的圖形化用戶界面的結(jié)構(gòu)中。

二、代碼

完整實例代碼點擊此處本站下載

github地址:https://github.com/liangdongchang/pycheckloginsys.git

1、init

用來初始化服務(wù):

①、在mysql上新建一個數(shù)據(jù)庫“homework”和建表”t_usr”

②、開啟redis服務(wù)程序

?
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
'''
@author ldc
'''
import os
import pymysql
'''
初始化服務(wù):
1、在mysql上新建一個數(shù)據(jù)庫“homework”和建表"t_usr"
2、開啟redis服務(wù)程序
'''
# 建立數(shù)據(jù)庫連接
conn = pymysql.connect(
  host='localhost',
  user='root',
  password="123456",
  port=3306
)
# 獲取游標(biāo)
cursor = conn.cursor()
# 創(chuàng)建數(shù)據(jù)庫
dbname = 'homework'
sql='''
   create database if not exists %s charset=utf8;
  '''%dbname
cursor.execute(sql)
# 使用數(shù)據(jù)庫
cursor.execute('use %s'%dbname)
# 創(chuàng)建表
sql = '''
  create table if not exists t_usr(
     id integer primary key auto_increment,
     username varchar(20) unique not null,
     password varchar(20) not null
    );
'''
cursor.execute(sql)
# 關(guān)閉游標(biāo)與連接
cursor.close()
conn.close()
# 開啟redis服務(wù),新建一個啟動redisd.bat文件,
#以后開啟redis服務(wù)就可以直接打開這個文件了
def openredisd(path):
  rpath = """@echo off
      redis-server %s
      pause"""%path
  with open(r"c:\users\ldcpc\desktop\啟動redisd.bat","w",encoding="ansi")
  as f:
   f.write(rpath)
openredisd(r"d:\ruanjian\redis-64.2.8.2101\redis.windows.conf")
# 打開文件“啟動redisd.bat”
os.popen(r"c:\users\ldcpc\desktop\啟動redisd.bat")

2、view層

用來與用戶交互:接收用戶的輸入和顯示結(jié)果給用戶。

?
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
'''
@author ldc
'''
from controller import urls
from model.model import user
from utils.dbutil import redisutil
'''
需求:登錄注冊驗證
1、登錄
2、注冊
3、改密
4、注銷
'''
# 主界面接口
def index():
  while true:
    #登錄界面
    print("********************************")
    print("*               *")
    print("*  (1) 登錄   (2)注冊   *")
    print("*  (3) 改密   (4)注銷   *")
    print("*      (5)退出      *")
    print("********************************")
    print()
    num = input("請輸入功能序號:")
    if num in ['1','2','3','4','5']:
      return num
    else:
      print("輸入有誤,請重新輸入!!!")
# 輸入賬號與密碼
def inputinfo():
  return input("請輸入賬號和密碼(逗號隔開):").split(',')
if __name__ == '__main__':
  # 連接redis數(shù)據(jù)庫
  redisutil.connect()
  while true:
    # 初始化界面
    num = index()
    # 輸入賬號密碼
    username, password = inputinfo()
    # 實例化一個用戶類
    user = user(username, password)
    if num == '1':
      urls.login(user) #登錄
    elif num == '2':
      urls.regist(user) # 注冊
    elif num == '3':
      urls.changepasswd(user) # 改密
    elif num == '4':
      urls.deleteuser(user) # 注銷
    else:
      break

3、controller層

實現(xiàn)業(yè)務(wù)邏輯,控制整個系統(tǒng)的實現(xiàn)流程。

?
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
'''
@author ldc
'''
from model.model import userdao
# 先查詢該用戶是否存在數(shù)據(jù)庫中
def exists(user):
  '''先查看redis緩存中是否有該用戶數(shù)據(jù)'''
  if not userdao.exists(user.username, 'redis'):
   '''然后在mysql中查詢該用戶是否存在'''
   if userdao.exists(user.username, 'mysql'):
     # 若在mysql存在就把該用戶寫進(jìn)redis,
     userdao.redis.set(user.username, user.password)
     return 'mysql'
   else :
     return none
  return 'redis'
'''
# 登錄模塊
先在redis上驗證,驗證成功則提示在redis上驗證成功
否則到mysql中驗證,驗證成功則提示在mysql上驗證成功
否則提示用戶不存在
'''
def login(user):
  print("------------登錄界面------------")
  # 查詢該用戶信息是否存在數(shù)據(jù)庫中
  wheredb = exists(user)
  if wheredb == 'redis':
   # 匹配密碼是否正確
   if userdao.query(user, 'redis') == user.password:
     print("[在redis中查詢到該用戶]登錄成功!!!")
     return 1
   else:
     print("[在redis中查詢到該用戶] 登錄失敗,用戶名或者密碼不正確!!!")
  elif wheredb == 'mysql':
   # 匹配密碼是否正確
   if userdao.query(user, 'mysql'):
     print("[在mysql中查詢到該用戶] 登錄成功!!!")
     return 1
   else:
     print("[在mysql中查詢到該用戶] 登錄失敗,用戶或者密碼不正確!!!")
  else:
   print("[在mysql中查詢不到該用戶]登錄失敗,該用戶不存在,請注冊后再登錄!!!")
  return 0
'''
# 注冊模塊
先在redis上查詢賬號是否存在,存在則注冊失敗
否則到mysql上查詢,用戶存在則注冊失敗
否則注冊成功,把賬號寫進(jìn)mysql,寫進(jìn)redis
'''
def regist(user):
  print("------------注冊界面------------")
  # 查詢該用戶信息是否存在數(shù)據(jù)庫中
  wheredb = exists(user)
  if wheredb :
   print("注冊失敗,該用戶已存在!!!")
  else:
   if userdao.insert(user):
     print("注冊成功!!!")
   else:
     print("注冊失敗!!!")
'''
# 修改密碼模塊
先在redis上和mysql上查詢,用戶存在就在mysql上修改該用戶密碼,
然后把該用戶信息重新寫進(jìn)redis中
在mysql中查詢不到該用戶,就返回該用戶不存在,改密失敗
'''
def changepasswd(user):
  print("------------改密界面------------")
  # 查詢該用戶信息是否存在數(shù)據(jù)庫中
  wheredb = exists(user)
  if wheredb:
   user.password = input("請輸入新密碼:")
   if userdao.changepasswd(user):
     print("改密成功!!!")
   else:
     print("改密失敗!!!")
  else:
   print("用戶不存在,改密失敗!!!")
'''
# 注銷用戶模塊
先在在redis上和mysql上查詢,用戶存在就在mysql和redis上刪除該用戶
在mysql中查詢不到該用戶,就返回該用戶不存在,注銷失敗
'''
def deleteuser(user):
  print("------------注銷界面------------")
  # 查詢該用戶信息是否存在數(shù)據(jù)庫中
  if login(user):
   if userdao.deleteuser(user):
     print("注銷成功!!!")
     return
  print("注銷失敗!!!")

4、model層

用來訪問數(shù)據(jù)庫,實現(xiàn)業(yè)務(wù)邏輯與數(shù)據(jù)庫分離,易于維護(hù)系統(tǒng)。

?
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
'''
@author ldc
'''
from utils.dbutil import redisutil, mysqlutil
# 用戶模型類
class user:
  def __init__(self,username,password):
    self.username = username
    self.password = password
# userdao
# 封裝了對user數(shù)據(jù)的增刪改查
# dao=database access object 數(shù)據(jù)庫訪問對象
class userdao:
  # 創(chuàng)建數(shù)據(jù)庫對象
  redis = redisutil()
  mysql = mysqlutil('homework','t_usr')
  # 執(zhí)行數(shù)據(jù)庫查詢操作,返回查詢結(jié)果
  @classmethod
  def query(cls,user,dbtype):
    datadict = {}
    datadict["username"] = user.username
    datadict["password"] = user.password
    if dbtype == 'redis':
      return cls.redis.get(user.username)
    elif dbtype == 'mysql':
      return cls.mysql.query(datadict)
  # 執(zhí)行數(shù)據(jù)庫查詢操作,查詢用戶是否存在,返回查詢結(jié)果
  @classmethod
  def exists(cls,username,dbtype):
    datadict = {}
    datadict["username"] = username
    if dbtype == 'redis':
      return cls.redis.exists(username)
    elif dbtype == 'mysql':
      return cls.mysql.exists(datadict)
    else:
      pass
  # 執(zhí)行數(shù)據(jù)插入操作,先把用戶信息添加進(jìn)mysql,然后再添加進(jìn)redis
  @classmethod
  def insert(cls, user):
    datadict = {}
    datadict["username"] = user.username
    datadict["password"] = user.password
    if cls.mysql.insert(datadict):
      cls.redis.set(user.username,user.password)
      return 1
    else:
      print("注冊失敗,服務(wù)器繁忙!!!")
      return 0
  # 修改密碼
  @classmethod
  def changepasswd(cls, user):
    datadict = {'changecol': 'password = %s'%user.password,
     'caluse' : 'username = %s'%user.username}
    if cls.mysql.update(datadict):
      cls.redis.set(user.username,user.password)
      return 1
    else:
      print("修改密碼失敗,服務(wù)器繁忙!!!")
      return 0
  # 注銷用戶
  @classmethod
  def deleteuser(cls, user):
    datadict = {'username' : user.username}
    if cls.mysql.delete(datadict):
      cls.redis.delete(user.username)
      return 1
    else:
      print("修改密碼失敗,服務(wù)器繁忙!!!")
      return 0

5、utils工具包

用來實現(xiàn)數(shù)據(jù)庫的增刪改查,可以被不同的系統(tǒng)調(diào)用。

?
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
'''
@author ldc
'''
import pymysql
import redis as redis
'''
mysql增刪改查操作類
'''
class mysqlutil:
  def __init__(self,dbname,tablename):
   self.dbname = dbname
   self.tablename = tablename
  # 連接數(shù)據(jù)庫,并生成全局可用的連接對象和查詢游標(biāo)
  def connect(self):
   self.conn = pymysql.connect(
     host='localhost', user='root', password="123456",
     database=self.dbname, port=3306,
   )
   self.cursor = self.conn.cursor()
  # 關(guān)閉全局游標(biāo),斷開全局連接
  def disconnect(self):
   self.cursor.close()
   self.conn.close()
  # 查詢用戶名是否存在
  def exists(self,datadict):
   caluse = ''
   for key,value in datadict.items():
     caluse += key + '="'+ value + '"'
   # print(caluse)
   sql = """
      select * from %s where %s ;
      """ % (self.tablename, caluse)
   return self.execute(sql)
  # 驗證用戶名和密碼是否正確
  def query(self, datadict):
   # 查詢子條件拼接
   caluse = ''
   for key, value in datadict.items():
     caluse += key + '="' + value + '" and '
   caluse = caluse[:-4]
   # print(caluse)
   sql = """
      select * from %s where %s;
      """% (self.tablename, caluse)
   return self.execute(sql)
  # 添加新用戶
  def insert(self, datadict):
   # sql語句拼接
   columns = ''
   values = ''
   for key, value in datadict.items():
     columns += key + ','
     values += '"' + value + '",'
   columns = columns[:-1]
   values = values[:-1]
   sql = """
      insert into %s (%s) values (%s);
      """ % (self.tablename, columns,values)
   # print(sql)
   return self.execute(sql)
  # 更新
  def update(self, datadict):
   # sql語句拼接
   changecol = datadict['changecol'] #要改變值的列名
   caluse = datadict['caluse'] #要改變值的子條件
   sql = 'update %s set %s where %s'%(self.tablename,changecol,caluse)
   return self.execute(sql)
  # 刪除
  def delete(self, datadict):
   # sql語句拼接
   caluse = ''
   for key,value in datadict.items():
     caluse += key + '="' + value + '"'
   sql = """
      delete from %s where %s;
      """ % (self.tablename,caluse)
   # print(sql)
   return self.execute(sql)
  # print(sql)
  # 執(zhí)行sql語句
  def execute(self, sql):
   self.connect()
   affected = 0
   try:
     affected = self.cursor.execute(sql)
   except baseexception as e:
     print(e)
     affected = 0
   finally:
     self.conn.commit()
     self.disconnect()
     return affected
'''
redis增刪改查操作類
'''
class redisutil:
  # redis連接
  @classmethod
  def connect(cls):
   cls.client = redis.redis(
     host='localhost', port=6379,
     db=1, password='123456',
   )
  # 判斷鍵是否存在
  @classmethod
  def exists(cls,key):
   return cls.client.exists(key)
  # 存儲鍵值,
  @classmethod
  def set(cls,key,value):
   # 鍵值存儲在緩存中,保留時間為30秒
   cls.client.setex(key,value,30)
  # 獲取鍵值
  @classmethod
  def get(cls,key):
   res = cls.client.get(key).decode("utf-8")
   return res
  # 刪除鍵值
  def delete(cls, key):
   cls.client.delete(key)

6、部分功能展示

注冊:

Python實現(xiàn)的登錄驗證系統(tǒng)完整案例【基于搭建的MVC框架】

登錄:

Python實現(xiàn)的登錄驗證系統(tǒng)完整案例【基于搭建的MVC框架】

改密:

Python實現(xiàn)的登錄驗證系統(tǒng)完整案例【基于搭建的MVC框架】

注銷:

Python實現(xiàn)的登錄驗證系統(tǒng)完整案例【基于搭建的MVC框架】

希望本文所述對大家python程序設(shè)計有所幫助。

原文鏈接:https://blog.csdn.net/lm_is_dc/article/details/80342510

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 男同gay玩奴男同玩奴 | 驯服有夫之妇HD中字日本 | 狠狠色伊人亚洲综合网站色 | 色综合天天综合中文网 | 禁漫H天堂免费A漫 | ysl千人千色t9t9t9t9 | 513热点网 | 美国video | 四虎在线免费 | www.青青草原 | www四虎 | 吉泽明步高清无码中文 | 国产欧美一区二区精品性色 | 32pao强力打造免费高速高 | 国产精品www | 波多野结衣在线免费观看 | 欧美国产日韩综合 | 免费观看在线 | 亚洲高清中文字幕精品不卡 | 亚洲男人天堂网站 | 国产精品久久久久久久久久久久 | 91精品91久久久久久 | 四虎在线成人免费网站 | 国产亚洲女在线线精品 | 成人区精品一区二区毛片不卡 | 久久综合色超碰人人 | 日韩精品一区二区三区中文版 | 性欧美高清强烈性视频 | 四虎成人免费 | 99久久综合 | 午夜爱爱片 | heyzo在线观看| 亚洲欧美日韩久久一区 | 欧美性野久久久久久久久 | 九九99亚洲精品久久久久 | 性欧美4khdxxxx | 被黑人日| 免费看视频 | 亚州春色 | 亚洲啊v| 国产精品亚欧美一区二区三区 |