自動化生成excl 而且郵件報警

#!coding:utf-8
##重慶話語科技---運維部--蓮花子
##python/shell
##羣號:35666534   歡迎你
import time
import signal
import random
import pymysql
import    xlsxwriter
workbook = xlsxwriter.Workbook('static/baoduym.xlsx')
worksheet = workbook.add_worksheet()
listhead=['id' ,  '域名' ,  '所屬部門'  , '360狀態','騰訊狀態','騰訊api狀態','檢測時間','新增時間']
row=1
col=0
for i in listhead:
            worksheet.write_string(row,col,i)
            col+=1
def myerror(a,b):
    errormail()
    workbook.close()
    raise

def datalook():
    row=2
    col=0
    department={
          '0':'未知',
          '1':'優效SSP&ADX項目部',
          '2':'優效分發-發行項目部',
          '3':'優效研發部',
          '4':'商業軟件矩陣',
          '5':'商業推广部',
          '6':'事業發展部',
          '7':'銷售部',
          '8':'移動廣告應用',
          '9':'優效商家項目部',
          '10':'總裁辦',
      }
    try:
        signal.signal(signal.SIGALRM,myerror)
        signal.alarm(200)
        print("...")
        cursor,con=connect()
        sql = "select * from dianxin_news_domains where status_360<>1 or status_qq <>1 or status_qqweb <>1;"
        cursor.execute(sql)
        result = cursor.fetchall()
    finally:
         pass
    for i in result:
        if i['status_360'] == 0:
                status_360='報毒'
        else:
                status_360='正常'
        if i['status_qq'] == 0:
                status_qq='報毒'
        else:
                status_qq='正常'
        if i['status_qqweb'] == 0:
                status_qqweb='報毒'
        else:
                status_qqweb='正常'
        worksheet.write_string(row,0,str(i['id']))
        worksheet.write_string(row,1,i['domain'])
        worksheet.write_string(row,2,department.get(str(i['department'])))
        worksheet.write_string(row,3,status_360)
        worksheet.write_string(row,4,status_qq)
        worksheet.write_string(row,5,status_qqweb)
        worksheet.write_string(row,6,time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(i['check_time'])))
        worksheet.write_string(row,7,time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(i['create_time'])))
        row+=1
    workbook.close()
    mail()
def connect():
    connection = pymysql.connect(host='8.8.8.8',
                             user='root',
                             password='',
                             db='news_beta',
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.DictCursor)
    cursor=connection.cursor()
    return cursor,connection



def mail():
        import smtplib
        from email.mime.multipart import MIMEMultipart
        from email.mime.text import MIMEText
        from email.mime.application import MIMEApplication
        server="smtp.exmail.qq.com"
        user="luohongjiang@yandui.com"
        password=""
        dstmail=["luohongjiang@yandui.com","mayongchuan@yandui.com","gaojinbo@yandui.com"]
        msg = MIMEMultipart()
        msg["Subject"] = "報毒域名"
        msg["From"]  = user
        msg["To"]   = ",".join(dstmail)
        part = MIMEText("報告!報毒域名,請查看附件 baoduym,如須要調整,請安排相關同事進行對接處理!")
        msg.attach(part)
        part = MIMEApplication(open('static/baoduym.xlsx','rb').read())
        part.add_header('Content-Disposition', 'attachment', filename="baoduym.xlsx")
        msg.attach(part)
        smtp=smtplib.SMTP(timeout=30)
        smtp.connect(server)
        smtp.helo
        smtp.login(user,password)
        smtp.sendmail(user, dstmail, msg.as_string())
        smtp.close()
        smtp.quit


datalook()
相關文章
相關標籤/搜索