Objective:
Add color printing function, realize the connection DB database login system, query, add, delete and login successfully after the implementation of the relevant operating system.
"User Login-add-delete, execute system command after successful login, failure prompt." Certified by MySQL database. print Color ' #encoding: utf8import osimport sysimport datetimeimport mysqldbimport timeimport shclass colored (object): # display format: \033[display mode; Foreground color m # write only one field to represent the foreground color, the back color default RED = ' \ 033[31m ' # red GREEN = ' \ 033[32m ' # green YELLOW = ' \033[33m ' # Yellow BLUE = ' \033[34m ' # Blue FUCHSIA = ' \033[35m ' # Purplish red CYAN = ' \033[36m ' # Blue WHITE = ' \033[37m ' # White #: no color reset = ' \033[0m ' # terminal default colors def Color_str (self, color, s): return ' {}{}{} ' . Format ( getattr (self, color ), s, self. reset ) def red (self, s): return self.color_str (' RED ', s) def green (self, s): Return self.color_str (' GREEN ', s) &nbsP;def yellow (self, s): return self.color_ STR (' YELLOW ', s) def blue (self, s): return self.color_str (' BLUE ', s) def Fuchsia (self, s): return self.color_str (' Fuchsia ', s) def cyan (self, s): return self.color_str (' CYAN ', s) def white (self, s): return self.color_str (' White ', s) class initdb (object): #conn =mysqldb.connect (host= ' 10.7.7.180 ', user= ' root ', Passwd= ' 123456 ', db= ' test ') #cur =conn.cursor () def __init__ ( Self,username= ' NULL ', passworD= ' NULL '): self.username=username self.password=password Self.conn=mysqldb.connect (host= ' 10.7.7.180 ', user= ' root ', passwd= ' 123456 ', db= ' test ') self.cur=self.conn.cursor () def add_user (self): itime=datetime.datetime.now () self.cur.execute ("insert into user (username, password,createtime) VALUES ('%s ', '%s ', '%s ') " % (self.username,self.password,itime)) self.cur.close () self.conn.commit () self.conn.close () return "Add user %s successful! " % self.username def login_check (self): self.cur.execute ("Select * from user where username= '%s ' and password = '%s ' % (Self.username,self.password) qur_result = self.cur.fetchall () if qur_result == (): return 0 else: return 1 self.cur.close () self.conn.close () def del_user ( Self): self.cur.execute ("Delete from user where username= '%s ' "&nbSp;% self.username) self.cur.close () self.conn.commit () Self.conn.close () def see_user (self): self.cur.execute ("Select * from user") Data = self.cur.fetchall () self.cur.close () self.conn.close () d = list (data) for x in d: print xwhile True: color = colored () print color.fuchsia (' * ' *50) choice=raw_input (Color.green ("Select operation options: \n\t[1] Add user \n\t[2] login user \n\t[3] Delete user \n\t[4] View user information \n\t[5] exit the program \ n Please select: ")) if choice == ' 1 ': inname=raw_input (Color.yellow ("Please enter the user name to add:") inpassword=raw_input (Color.yellow ("Enter the password you want to add for%s:" % inname )) initdb (Inname,inpassword). Add_user () print color.blue ("User%s added successfully!") % inname) elif choice == ' 2 ': username=raw_input (Color.yellow ("Please enter user name:")) Password=raw_input (Color.yellow ("Please enter password:")) f=initdb (username, Password). Login_check () if f == 0: print cOlor.red ("login faild!please check your username and password!") elif f == 1: print color.green ("login successful!\nhello,mr %s" % username) time.sleep (3) filelist=open ('/etc/ passwd ') for i in Filelist.readlines (): print i time.sleep (3) print sh.ifconfig (' Ens33 ') &nBSP;  PRINT SH.DF ('-h ') Print sh.free ('-M ') print Sh.tail (' -100 ', '/var/log/auth.log ') else: print "something wrong!" elif choice == ' 3 ': Dname=raw_input (Color.yellow ("Enter the user name to delete:")) initdb (dname). del _user () print Color.Red ("User%s deleted successfully!") % dname) elif choice == ' 4 ': initdb (). See_user () elif choice == ' 5 ': print color.green ("The program has exited, welcome to the next visit!") break else: print color.red ("unknown option, please re-enter!")
This article is from the "Can't Grow" blog, be sure to keep this source http://tfbaby.blog.51cto.com/3125450/1902945
Python Simple DB authentication system