py监控数据库最新数据并通过邮件发送数据

# -*- coding=utf-8 -*-
import os
import sys
import smtplib
import common
import MySQLdb
import warnings
import datetime
import time
from smtplib import SMTP_SSL
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
 
warnings.filterwarnings("ignore")
 
#os.system("""source /etc/profile;/usr/bin/mysql -h127.0.0.1 -P3306 -uzzhS -pHZmysql_SDB0814#! -Ddb_mxh_time -e"SELECT uid,name,phone_num,qq,area,created_time FROM match_apply where match_id = 83;">/root/MailMysqlData/huoyingrenzhe_enroll_info.txt""")
 
 
db_config = {
    'host': '127.0.0.1',
    'user': 'zzhS',
    'passwd': 'HZmysql_SDB0814#!',
    'port': 3306,
    'db': 'db_mxh_time'
}
 
 
def getDB():
    try:
        conn = MySQLdb.connect(host=db_config['host'], user=db_config['user'], passwd=db_config['passwd'], port=db_config['port'])
        conn.autocommit(True)
        curr = conn.cursor()
        curr.execute("SET NAMES utf8");
        curr.execute("USE %s" % db_config['db']);
 
        return conn, curr
    except MySQLdb.Error, e:
        print "Mysql Error %d: %s" % (e.args[0], e.args[1])
        return None, None
 
 
conn, curr = getDB()
 
today = datetime.date.today()
yesterday = today - datetime.timedelta(days=1)
tomorrow = today + datetime.timedelta(days=1)
 
sql_text = "select * from tb_mxg_trade_log where tradeTime between date_add(now(), interval - 30 minute) and now();"
curr.execute(sql_text)
html_data = "<tr style='font-weight:bold;'><td>用户ID</td><td>姓名</td><td>电话号码</td></tr>"
items = curr.fetchall()
for item in items:
    print item
    item0 = item[1]
    item1 = item[2]
    item2 = item[3]
 
    html_data += "<tr><td>%s</td><td>%s</td><td>%s</td></tr>" % (
    item0, item1, item2)
 
curr.close()
conn.close()
 
mail_user = "17682328075@163.com"
mail_pass= "word2613182"
mailto_list = ["992565438@qq.com"]
mail_host="smtp.163.com"
msg = MIMEMultipart('alternatvie')
msg['Subject'] = Header("火影校园赛赛事实时报名-按小时汇报", "utf-8")  # 组装信头
msg['From'] = mail_user  # 使用国际化编码
msg['To'] = ', '.join(mailto_list)
 
html = "Hi All:<br>这是最新的火影校园赛赛事实时报名-按小时汇报信息,每小时全量发送播报一次,请查收!<br><br><table border='1' style='background-color:#22B8DD'>" + html_data + "</table>"
html_part = MIMEText(html, 'html')  # 实例化为html部分
html_part.set_charset('utf-8')  # 设置编码
msg.attach(html_part)  # 绑定到message里
 
# 构造附件
#att_path = "/root/MailMysqlData/huoyingrenzhe_enroll_info.txt"
#att_file_name = "attachment; filename=\"huoyingrenzhe_enroll_info.txt\""
#att = MIMEText(open(att_path, 'rb').read(), 'base64', 'utf-8')
#att["Content-Type"] = 'application/octet-stream'
#att["Content-Disposition"] = att_file_name
#msg.attach(att)
 
try:
    s = SMTP_SSL()  # 登录SMTP服务器,发信
    s.connect(mail_host)
    s.login(mail_user, mail_pass)
    s.sendmail(mail_user,mailto_list,msg.as_string())
except Exception, e:
    print e
#print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),"Mail Send Finished!"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值