shell中调用python函数,发送邮件
shell,调用,python,函数,发送,邮件
2025-03-20 08:49:00 时间
一、shell中调用python函数
1.邮件正文是框架自带的生成的报告
2.邮件附件是第三方类库生成的炫酷的报告看板
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_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函数
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
相关文章
- Python-drf前戏38-前端Vue
- Python基础13-模块的使用
- Python元祖详解
- Python 链接/操作 MongoDB 数据库
- Python嵌套函数与匿名函数
- 使用 python 执行 shell 命令的几种常用方式
- python不报错但计算不出结果_excel表格不能用公式怎么办
- dataframe loc iloc_python的isnull函数
- Python进阶39-drf框架(一)
- sklearn cross validation_python sklearn
- Python版PHP内置的MD5()函数
- Python进阶40-drf框架(二)
- Python 命令行参数的3种传入方式
- Python基础14-内置模块
- 用Python的好处
- python win32api messagebox_如何在Python中使用Win32 API?
- 2022年最新Python大数据之Python基础【九】面向对象与继承
- 【水水水文章】用 Python 发邮件
- .app 域名发布了,我们可以使用 Python 做点什么?
- python 字符串(字符序列)和字节序列