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