码迷,mamicode.com
首页 > 其他好文 > 详细

通过邮件发送running process输出最后N行

时间:2015-07-17 22:56:52      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:python   running   process   

#!/usr/bin/python
#coding: utf-8
import sys
import smtplib
from email.mime.text import MIMEText
import re,commands

def lastline():
    global pos
    while True:
        pos = pos - 1
        try:
            f.seek(pos, 2)
            if f.read(1) == ‘\n‘:
                break
        except:
            f.seek(0, 0)
            return f.readline().strip()
    return f.readline().strip()

def mail_send(text=‘‘,subject=‘未定义主题‘):

        mail_host = "xxx"
        mail_user = "xxx"
        mail_pass = "xxx"
        mail_list_to = ["xxx","xxx"]
        mail_from = "xxx"
        msg = MIMEText(str(text),"html","utf-8")
        msg[‘Subject‘] = subject
        msg[‘From‘] = mail_from
        msg[‘To‘] = ",".join(mail_list_to)

        try:
                server = smtplib.SMTP()
                server.connect(mail_host,"25")
                server.starttls()
                server.login(mail_user,mail_pass)
                server.sendmail(mail_from,mail_list_to,msg.as_string())
                server.quit()
                print "邮件发送成功!"
        except Exception,e:
                print "失败:"+str(e)

if __name__ == "__main__":
        process = commands.getoutput(‘xxx‘)
        flag = re.search(‘xxx‘,process)
        if flag:
                f = open(‘xxx‘,‘r‘)
                pos = 0
                log = []
                for line in range(1000):
                        log.append(lastline())
                log.reserse()
                text1 = log
                sub = ‘xxx‘
                print text1
                f.close()
                mail_send(text1,sub)


本文出自 “大成” 博客,请务必保留此出处http://liuchw.blog.51cto.com/6858799/1675763

通过邮件发送running process输出最后N行

标签:python   running   process   

原文地址:http://liuchw.blog.51cto.com/6858799/1675763

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!