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

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

一、shell中调用python函数

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

image.png

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

image.png

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 = 'xxxxxxxxxxxxx@126.com'
    password = 'xxxxxxxxxQYIAPTAQST'
    receicer = ['gaojs@xxxxxxxxxxx.com.cn', 'xxxxxxxxxxxxxx@163.com']
    # receicer = ['gaojs@xxxxxxxxxxx.com.cn', '13152xxxxx6@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
相关文章
|
8天前
|
Python
Python闭包函数和计时器
本文介绍了闭包函数的概念,它允许内部函数引用外部作用域的变量但无法修改它们。示例展示了如何使用闭包来封装函数。接着,文章讨论了如何在函数调用时添加开始和结束的打印语句,通过传递函数作为参数实现。然后,文章引入装饰器,通过闭包定义了一个`timer`装饰器,用于在函数执行前后打印消息。最后,给出了一个练习,实现了一个计算函数执行时间的装饰器,处理了带有参数的被装饰函数。
22 1
|
1天前
|
Python
|
2天前
|
算法 数据可视化 数据处理
Python基础教程——函数
Python基础教程——函数
|
2天前
|
Python
Python基础 笔记(九) 函数及进阶
Python基础 笔记(九) 函数及进阶
21 6
|
2天前
|
Python
Python基础 笔记(三) 标识符、输入输出函数
Python基础 笔记(三) 标识符、输入输出函数
17 7
|
2天前
|
Python
Python 新版本有75个内置函数,你不会不知道吧(1)
Python 新版本有75个内置函数,你不会不知道吧(1)
Python 新版本有75个内置函数,你不会不知道吧(1)
|
5天前
|
程序员 开发者 Python
Python中的装饰器:优雅而强大的函数修饰工具
在Python编程中,装饰器是一种强大的工具,它可以简洁地实现函数的增强、扩展和重用。本文将深入探讨Python中装饰器的工作原理、常见应用场景以及如何自定义装饰器,帮助读者更好地理解和运用这一重要的编程概念。
|
5天前
|
数据采集 Python
10个Python set 常用操作函数!,bilibili面试题
10个Python set 常用操作函数!,bilibili面试题
10个Python set 常用操作函数!,bilibili面试题
|
5天前
|
数据采集 数据挖掘 Python
Python学习——函数,2024年最新手持4个大厂offer的我
Python学习——函数,2024年最新手持4个大厂offer的我
|
5天前
|
数据采集 数据挖掘 关系型数据库
Excel计算函数(计算机二级)(1),2024年最新2024Python架构面试指南
Excel计算函数(计算机二级)(1),2024年最新2024Python架构面试指南