前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >shell中调用python函数,发送邮件

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

作者头像
懿曲折扇情
修改于 2022-09-20 04:52:12
修改于 2022-09-20 04:52:12
86600
代码可运行
举报
文章被收录于专栏:建帅技术分享建帅技术分享
运行总次数:0
代码可运行

一、shell中调用python函数

1.邮件正文是框架自带的生成的报告

2.邮件附件是第三方类库生成的炫酷的报告看板

send_email.py

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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_xxxx@126.com'
    password = 'xxxxxxxxxQYIAPTAQST'
    receicer = ['gaojs@000000000.com.cn', '13152020000@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函数

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
python3 -c 'import send_email; print(send_email.send_email())'

run.sh

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# 删除上次产生的报告
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
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、shell中调用python函数
    • send_email.py
    • sh文件中调用send_mail函数
    • run.sh
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档