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
相关文章
|
3天前
|
数据挖掘 数据处理 索引
python常用pandas函数nlargest / nsmallest及其手动实现
python常用pandas函数nlargest / nsmallest及其手动实现
19 0
|
15天前
|
Python
python函数的参数学习
学习Python函数参数涉及五个方面:1) 位置参数按顺序传递,如`func(1, 2, 3)`;2) 关键字参数通过名称传值,如`func(a=1, b=2, c=3)`;3) 默认参数设定默认值,如`func(a, b, c=0)`;4) 可变参数用*和**接收任意数量的位置和关键字参数,如`func(1, 2, 3, a=4, b=5, c=6)`;5) 参数组合结合不同类型的参数,如`func(1, 2, 3, a=4, b=5, c=6)`。
16 1
|
30天前
|
Python
Python函数使用(四)
Python函数使用(四)
63 0
|
2天前
|
运维 Shell Python
Shell和Python学习教程总结
Shell和Python学习教程总结
|
8天前
|
Serverless 开发者 Python
《Python 简易速速上手小册》第3章:Python 的函数和模块(2024 最新版)
《Python 简易速速上手小册》第3章:Python 的函数和模块(2024 最新版)
40 1
|
8天前
|
索引 Python
Python高维变量选择:SCAD平滑剪切绝对偏差惩罚、Lasso惩罚函数比较
Python高维变量选择:SCAD平滑剪切绝对偏差惩罚、Lasso惩罚函数比较
10 0
|
10天前
|
Python
python学习-函数模块,数据结构,字符串和列表(下)
python学习-函数模块,数据结构,字符串和列表
53 0
|
10天前
05-python之函数-函数的定义/函数的参数/函数返回值/函数说明文档/函数的嵌套使用/函数变量的作用域
05-python之函数-函数的定义/函数的参数/函数返回值/函数说明文档/函数的嵌套使用/函数变量的作用域
|
11天前
|
Python
python学习10-函数
python学习10-函数
|
11天前
|
Python
python学习4-内置函数range()、循环结构、循环控制语句、else语句、嵌套循环
python学习4-内置函数range()、循环结构、循环控制语句、else语句、嵌套循环