没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
Python实现的查询实现的查询mysql数据库并通过邮件发送信息功能数据库并通过邮件发送信息功能
主要介绍了Python实现的查询mysql数据库并通过邮件发送信息功能,可实现Python针对mysql的查询与宕机信息通过邮件发送的相关操作技巧,需要的朋友可以参考下
本文实例讲述了Python实现的查询mysql数据库并通过邮件发送信息功能。分享给大家供大家参考,具体如下:
这里使用Python查询mysql数据库,并通过邮件发送宕机信息。
Python代码如下:
#-*- coding: UTF-8 -*-
#!/usr/bin/env python
'''''
author:qlzhong
Created on 2015-6-29
征途宕机日志统计汇总
'''
import MySQLdb
import time
import datetime
import smtplib
from email.mime.text import MIMEText
mailto_list=["mail@mail.com"]
#mailto_list=["zhongqilong@ztgame.com"]
mail_host="smtp.qq.com" #设置服务器
mail_user="" #用户名
mail_pass="" #口令
mail_postfix="" #发件箱的后缀
def send_mail(to_list,sub,content):
me="hello"+"<"+mail_user+"@"+mail_postfix+">"
msg = MIMEText(content,_subtype='plain',_charset='utf-8')
msg['Subject'] = sub
msg['From'] = me
msg['To'] = ";".join(to_list)
try:
server = smtplib.SMTP()
server.connect(mail_host)
server.login(mail_user,mail_pass)
server.sendmail(me, to_list, msg.as_string())
server.close()
return True
except Exception, e:
print str(e)
return False
class MySQLHelper:
#配置数据库信息并连接
def __init__(self,host="****",user="****",password="****",port=3306,charset="utf8"):
self.host=host
self.user=user
self.password=password
self.port=port
self.charset=charset
try:
self.conn=MySQLdb.connect(host=self.host,user=self.user,passwd=self.password,port=self.port)
self.conn.set_character_set(self.charset)
self.cur=self.conn.cursor()
print("==================connect success====================")
except MySQLdb.Error as e:
print("Mysql Error %d: %s" % (e.args[0], e.args[1]))
#取出需要统计的数据库名称
def db_name(self):
un_db_name = ['information_schema','cz','ecshop','edutone','gz','mysql','newparent','parent','performance_schema','test','xx','yyhd']
name = []
try:
self.cur.execute('show databases')
for row in self.cur.fetchall():
for i in row:
if i not in un_db_name:
name.append(i)
return name
except MySQLdb.Error as e:
print("Mysql Error %d: %s" % (e.args[0], e.args[1]))
#指定查询的数据库名称
def selectDb(self,db):
try:
self.conn.select_db(db)
except MySQLdb.Error as e:
print("Mysql Error %d: %s" % (e.args[0], e.args[1]))
#使用该语句来直接查询昨天和今天的差异
def monion_today_yesddiff(self, today, yestoday):
try:
strresult = ""
strsql = 'SELECT address, charversion, sum(today) as today, sum(yesterday) as yesterday '
strsql += 'FROM (SELECT address, "" as today, tmp as yesterday, charversion FROM ( SELECT count(*) As tmp, address, charversion From `' + yestoday
strsql += '` WHERE charversion like \'1.0.0.3%\' GROUP BY address) As TEST WHERE tmp>=50 '
strsql += ' union all '
strsql += 'SELECT address, tmp as today, "" as yesterday, charversion FROM (SELECT count(*) As tmp, address, charversion From `'
strsql += today
strsql += '` WHERE charversion like \'1.0.0.3%\' GROUP BY address) As TEST WHERE tmp>=50 ) As Diff GROUP BY address, charversion'
print(strsql + "")
self.cur.execute(strsql)
name_list = [tuple[0] for tuple in self.cur.description]
strresult += str(name_list) + ""
# for row in self.cur.fetchall():
# return row
s = self.cur.fetchall()
todaynum = 0
yestodaynum = 0
for col in s:
strresult += str(col[0]) + " " + str(col[1]) + " " + str(col[2]) + " " + str(col[3]) + ""
todaynum += int(col[2])
yestodaynum += int(col[3])
strresult += "今日宕机总数:" + str(todaynum) + " 昨日宕机总数:" + str(yestodaynum) + " 同昨日相比增加: " + str(todaynum - yestodaynum) + ""
return strresult
except MySQLdb.Error as e:
print("Mysql Error:%s" %(e))
def close(self):
self.cur.close()
self.conn.close()
todayrang = 0;
yestodayrang = 0;
#按照范围查询
def monion_rang_today_yesddiff(self, today, yestoday, num1, num2):
try:
strresult = ""
strsql = 'SELECT sum(today) as today, sum(yesterday) as yesterday FROM (SELECT "" as today, tmp as yesterday FROM ( SELECT count(*) As tmp From `' + yestoday
strsql += '` WHERE charversion like \'1.0.0.3%\' GROUP BY address) As TEST WHERE tmp<' + str(num2) + ' AND tmp>=' + str(num1) + ' union all '
strsql += 'SELECT tmp as today, "" as yesterday FROM (SELECT count(*) As tmp From `' + today + '` WHERE charversion like \'1.0.0.3%\' GROUP BY address) As TEST WHERE tmp<' + str(num2) + ' AND tmp>=' + str(num1) + ' ) As Diff'
print(strsql + "")
self.cur.execute(strsql)
name_list = [tuple[0] for tuple in self.cur.description]
#strresult += str(name_list) + ""
# for row in self.cur.fetchall():
# return row
s = self.cur.fetchall()
资源评论
weixin_38557068
- 粉丝: 4
- 资源: 863
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- record record record record record record record record record
- Android 启动提示Android 正在升级...提示源码分析
- pojie-drawio-confluence-plugin-9.5.8.obr
- 信息学奥赛2020年NOIP真题
- SunloginClient-15.1.0.58718-x64.exe
- 信息学奥赛2021年NOIP真题
- 星辰语义大模型TeleChat超详细部署文档手册
- python range函数.pdf
- 许子昊毕业论文初稿-基于响应式编程的水质设备监测平台的设计与实现-定稿格式.docx
- musicManager.jsp
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功