python查询mysql导出结果至Excel并发送邮件

#!/usr/bin/python
#coding: utf-8
import sys
import xlwt
import MySQLdb
import datetime
import time
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
import os.path

host = 'localhost'
user = 'user'
pwd = 'Passwd'
port = 3306
db = 'database'
sheet_name = 'report' + time.strftime("%Y-%m-%d")
filename = 'report_' + time.strftime("%Y-%m-%d") + '.xls'
out_path = '/data/monitor/temp/report_'+ time.strftime("%Y-%m-%d") + '.xls'
print(out_path)
sql = 'Select * from sys_user;'
def export():
    conn = MySQLdb.connect(host,user,pwd,db,charset='utf8')
    cursor = conn.cursor()
    count = cursor.execute(sql)
    print("查询出" + str(count) + "条记录")
    if count>0:
        #来重置游标的位置
        cursor.scroll(0,mode='absolute')
        #搜取所有结果
        results = cursor.fetchall()
        # 获取MYSQL里面的数据字段名称
        fields = cursor.descripti

你可能感兴趣的:(Python,OpsDev运维开发)