目 录CONTENT

文章目录

shell中调用python函数,发送邮件

懿曲折扇情
2022-07-16 / 0 评论 / 16 点赞 / 103 阅读 / 569 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-07-24,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。
广告 广告

一、shell中调用python函数

1.邮件正文是框架自带的生成的报告
image-1657950506534
2.邮件附件是第三方类库生成的炫酷的报告看板
image-1657950487576

send_email.py

import re
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from smtplib import SMTP_SSL
from email.header import Header
import schedule
from selenium import webdriver
from email.mime.text import MIMEText
from selenium.webdriver.chrome.options import Options


def get_report_source_code():
    """
    获取test报告源码页面
    """
    url = 'http://[192::1:192]/cgi-bin/test_report.pl?build=netIAG_3_2_0_13_gaojs_716'
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')
    driver = webdriver.Chrome(options=chrome_options)
    driver.get(url)
    driver.maximize_window()
    source_code = driver.page_source
    return source_code
    
    
def send_email():
    """
    发送test_report邮件
    """
    # 获取页面源码
    source_code = get_report_source_code()
    # 以126邮箱为例
    # ----------------发件相关参数----------------
    smtpserver = 'smtp.126.com'
    port = 0
    sender = 'testops_jianshuai@126.com'
    password = 'xxxxxxxxxQYIAPTAQST'
    receicer = ['gaojs@arraynetworks.com.cn', '13152027756@163.com']
    # receicer = ['sunyb@arraynetworks.com.cn', 'gaojs@arraynetworks.com.cn',
    #             'gesk@arraynetworks.com.cn', 'leixc@arraynetworks.com.cn',
    #             'linn@arraynetworks.com.cn', '13152027756@163.com']
    # ----------------编辑邮件内容----------------
    subject = 'netIAG每日构建测试报告'
    body = f'<p>{source_code}<p>'
    msg = MIMEMultipart()
    msg['Subject'] = Header(subject, 'utf-8')  # 邮件主题
    msg['From'] = sender  # 发件人
    msg['To'] = ';'.join(receicer)
    msg.attach(MIMEText(body, 'html', 'utf-8'))
    attchment = MIMEApplication(open(r'./reports/report.html', 'rb').read())
    attchment.add_header('Content-Disposition', 'attachment', filename='report.html')
    msg.attach(attchment)  # 添加附件到邮件

    smtp = SMTP_SSL(smtpserver)
    smtp.login('testops_jianshuai@126.com', password)
    smtp.sendmail(sender, receicer, msg.as_string())
    smtp.quit()
    print('******************* 邮件发送完成 ,请查收附件************************')


if __name__ == '__main__':
    send_email()

sh文件中调用send_mail函数

python3 -c 'import send_email; print(send_email.send_email())'

run.sh

# 删除上次产生的报告
rm -rf /home/array/src/reports/*

pytest -s ./smoke_test/aaa/radius/ --build netIAG_2_2_0_13_gaojs_716 --report=report.html --title=netIAG3.2.0.13每日构建测试报告 --tester=gaojs --desc=netIAG每>日构建报告  --template=2

# 将产生的报告重命名为report.html
mv /home/array/src/reports/*report.html /home/array/src/reports/report.html

# 调用发邮件函数
cd /home/array/src/

python3 -c 'import send_email; print(send_email.send_email())'

sleep 15s

echo ""
echo "test done
16

评论区