前言:
添加颜色打印功能,实现了连接db数据库登陆系统,查询,添加,删除以及登录成功后执行相关操作系统。
‘‘‘ 用户登录-添加-删除,登陆成功后执行系统命令,失败提示。通过mysql数据库认证。 打印color ‘‘‘ #encoding:utf8 import os import sys import datetime import MySQLdb import time import sh class Colored(object): # 显示格式: \033[显示方式;前景色;背景色m # 只写一个字段表示前景色,背景色默认 RED = ‘\033[31m‘ # 红色 GREEN = ‘\033[32m‘ # 绿色 YELLOW = ‘\033[33m‘ # 黄色 BLUE = ‘\033[34m‘ # 蓝色 FUCHSIA = ‘\033[35m‘ # 紫红色 CYAN = ‘\033[36m‘ # 青蓝色 WHITE = ‘\033[37m‘ # 白色 #: no color RESET = ‘\033[0m‘ # 终端默认颜色 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) 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‘" % 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 x while True: color = Colored() print color.fuchsia(‘*‘*50) choice=raw_input(color.green("请选择操作选项:\n\t[1]添加用户\n\t[2]登陆用户\n\t[3]删除用户\n\t[4]查看用户信息\n\t[5]退出程序\n请选择:")) if choice == ‘1‘: inname=raw_input(color.yellow("请输入要添加的用户名称:")) inpassword=raw_input(color.yellow("请输入要为%s添加的密码:" % inname)) initdb(inname,inpassword).add_user() print color.blue("用户%s添加成功!" % inname) elif choice == ‘2‘: username=raw_input(color.yellow("请输入用户名:")) password=raw_input(color.yellow("请输入密码:")) 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‘) 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("请输入要删除的用户名称:")) initdb(dname).del_user() print color.red("用户%s删除成功!" % dname) elif choice == ‘4‘: initdb().see_user() elif choice == ‘5‘: print color.green("程序已退出,欢迎下次光临!") break else: print color.red("未知的选项,请重新输入!")
本文出自 “无法长大” 博客,请务必保留此出处http://tfbaby.blog.51cto.com/3125450/1902945
原文地址:http://tfbaby.blog.51cto.com/3125450/1902945