zoukankan      html  css  js  c++  java
  • python 自动化实现定时发送html报告到邮箱

    # coding =utf-8
    import os
    import unittest
    import time
    import datetime
    import smtplib
    from email.mime.text import MIMEText
    from HTMLTestRunner import HTMLTestRunner
    
    
    def all_cases():
            case_path = os.getcwd()
    
            discover = unittest.defaultTestLoader.discover(case_path, pattern='test*.py', top_level_dir=None)
            return discover
    
    
    def run(report_path):
            with open(report_path, 'wb') as f:
                runner = HTMLTestRunner(stream=f, title="interface report", description="results like following:", verbosity=2)
                runner.run(all_cases())
            f.close()
    
    
    def timer(report_path, hour, minute):
        flag = 1
        while flag:
            while flag:
                now = datetime.datetime.now()
                if now.hour == hour and now.minute == minute:
                    break
                time.sleep(10)
            # run(report_path)
            email_send(report_path)
            flag = 0
    
    
    def email_send(report_path,str_sender,receiver,author_code,smtp_server="smtp.qq.com",smtp_int_port=465):
            run(report_path)
            msg_from = str_sender  # sender
            passwd = author_code     # authentication  code
            msg_to = receiver    # receiver
            #如果多个接受者用列表 ['1932390299@qq.com','1393232463@qq.com']
            subject = "python_email_test"
            f = open(report_path, 'rb')
            mail_body = f.read()
            f.close()
            msg=MIMEText(mail_body, _subtype='html', _charset='utf-8')
    
            msg['Subject'] = subject
            msg['From'] = msg_from
            msg['To'] = msg_to
            try:
                s = smtplib.SMTP_SSL(smtp_server, smtp_int_port)
                s.login(msg_from, passwd)
                s.sendmail(msg_from, msg_to, msg.as_string())
                print("send success")
            except smtplib.SMTPException as e:
                print("send fail")
            finally:
                s.quit()
    
    def get_report_path():
            job_name = time.strftime('job_%Y%m%d%H%M%S', time.localtime()) + '.html'
            path = os.path.join(os.path.dirname(__file__), 'report')
            report_path = os.path.join(path, job_name)
            return  report_path

     上面是很久以前的没更新:邮件发送见最新的文章封装很全面写了一次https://www.cnblogs.com/SunshineKimi/p/10629267.html

    定时计划参考最新的机制schedule文章  :https://www.cnblogs.com/SunshineKimi/p/10630784.html

    报告选型见我的报告模块:https://www.cnblogs.com/SunshineKimi/category/1426942.html

  • 相关阅读:
    String方法
    多态
    观察者模式
    ArrayList和LinkList
    唐岛湾
    AForge.Net C#的操作视频,照片读写功能
    JqGrid填坑
    日常点滴
    日常点滴
    EF Core 填坑记录
  • 原文地址:https://www.cnblogs.com/SunshineKimi/p/10540182.html
Copyright © 2011-2022 走看看