寫了一個(gè)簡(jiǎn)單的登錄驗(yàn)證過程
成都創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站建設(shè)、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的恭城網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
首先創(chuàng)建目錄結(jié)構(gòu)
model 是操作數(shù)據(jù)庫表相關(guān)的 admin.py是對(duì)應(yīng)于MySQL 數(shù)據(jù)庫中的數(shù)據(jù)庫member下的admin表
utility是關(guān)于操作數(shù)據(jù)庫相關(guān)的
conf.py 文件是一些配置字符串
index.py 是程序前端入口
首先index.py中的代碼:
#!/usr/bin/env python #coding:utf-8 from model.admin import Admin def main(): user = raw_input('inpute your username:') pawd = raw_input('inpute your password:') admin = Admin() result =admin.CheckValiData(user, pawd) if not result : print "username or password not right!" else: print "%s login success" % user if __name__== '__main__': main()
通過Admin類查找用戶名和密碼:
admin.py文件中的代碼:
#!/usr/bin/env python #coding:utf-8 from utility.SqlHelper import MySqlHelper class Admin(object): def __init__(self): self.__helper= MySqlHelper() def CheckValiData(self,username,password): sql="select * from admin where name=%s and password=%s" parmars=(username,password,) return self.__helper.Get_One(sql, parmars)
Admin類調(diào)用MySqlHelper類來操作數(shù)據(jù)庫
SqlHelper.py文件中的代碼:
#!/usr/bin/env python #coding:utf-8 import MySQLdb import conf class MySqlHelper(object): def __init__(self): self.__dict=conf.db_dict def Get_One(self,sql,parmars): conn = MySQLdb.connect(**self.__dict) cur= conn.cursor() recount = cur.execute(sql,parmars) data = cur.fetchone() cur.close() conn.close() return data
conf.py文件中的代碼:
#!/usr/bin/env python #coding:utf-8 db_dict = dict(host='127.0.0.1',user='root',passwd='redhat',db='member')
mysql數(shù)據(jù)庫中的admin表的內(nèi)容如下:
mysql> select * from admin; +----+------+----------+ | id | name | password | +----+------+----------+ | 1 | tom | 123 | | 2 | jack | 1234 | +----+------+----------+ 2 rows in set (0.00 sec) mysql>
執(zhí)行python index.py輸出結(jié)果如下:
[root@web Mysqlhelper]# python index.py inpute your username:tom inpute your password:123 tom login success [root@web Mysqlhelper]# python index.py inpute your username:jack inpute your password:12334 username or password not right! [root@web Mysqlhelper]#