Simple login verification
Write a simple login verification process
First create the directory structure
Model is related to the operation of database tables. Admin.py corresponds to the admin table under database member in the mysql database.
Utility is about manipulating databases.
Conf.py files are configuration strings
Index.py is the front entrance to the program.
First, the code in index.py:
#! / usr/bin/env python#coding:utf-8from model.admin import Admindef 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"% userif _ _ name__=='_ _ main__': main ()
Find the user name and password through the Admin class:
The code in the admin.py file:
#! / usr/bin/env python#coding:utf-8from utility.SqlHelper import MySqlHelperclass 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)
The Admin class calls the MySqlHelper class to operate the database
The code in the SqlHelper.py file:
#! / usr/bin/env python#coding:utf-8import MySQLdbimport confclass 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
The code in the conf.py file:
# / usr/bin/env python#coding:utf-8db_dict = dict (host='127.0.0.1',user='root',passwd='redhat',db='member')
The contents of the admin table in mysql database are as follows:
Mysql > select * from admin;+----+ | id | name | password | +-- + | 1 | tom | 123 | 2 | jack | 1234 | +-+ 2 rows in set (0.00 sec) mysql >
The output result of executing python index.py is as follows:
[root@web Mysqlhelper] # python index.pyinpute your username:tominpute your password:123tom login success [root@web Mysqlhelper] # python index.pyinpute your username:jackinpute your password:12334username or password not right! [root@web Mysqlhelper] #