2022-08-24 11:49:53
浏览数 (1)
获取httpserver 资源,展示在邮件中
代码语言:javascript
复制#!/bin/bash
# coding=utf-8
"""
作者:gaojs
功能:
新增功能:
schedule 是一个第三方轻量级的任务调度模块,可以按照秒,分,小时,日期或者自定义事件执行时间。
schedule 允许用户使用简单、人性化的语法以预定的时间间隔定期运行 Python 函数(或其它可调用函数)
demo如下:
schedule.every(10).seconds.do(job)
schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)
schedule.every(5).to(10).minutes.do(job)
schedule.every().monday.do(job)
schedule.every().wednesday.at("13:15").do(job)
schedule.every().minute.at(":17").do(job)
日期:2022/5/29 12:45
blog: www.gaojs.com.cn
"""
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.firefox.options import Options
import re
import time
import os
def get_report_source_code():
"""
获取test报告源码页面
"""
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Firefox(options=chrome_options)
release_name = ['APV', 'netIAG', 'FT', 'UAG', 'AG9.4']
source = ''
for release in release_name:
time.sleep(5)
url = f'http://192.168.120.204/builds/{release}/'
driver.get(url)
driver.maximize_window()
source_code = driver.page_source
# 删除网页源码部分标签
result = re.sub('<td valign="top">(.*?)</td>', ' ', source_code)
result = re.sub('<tr> <td><a href="/builds/">Parent(.*?)</td></tr>', ' ', result)
result = re.sub('<address>(.*?)</address>', ' ', result)
result = re.sub('<h1>(.*?)</h1>', f'<h1>{release} Latest Builds</h1>', result)
result1 = re.sub('<tr><th valign="top">(.*?)</tr>', ' ', result)
r = re.findall('<tr> <td><a href=".*?">(.*?)</a></td>', result1)
if r:
s1 = result1
for i in r:
t = f'http://192.168.120.204/builds/{release}/' i
s0 = re.sub(f'<tr> <td><a href=".*?">{i}</a></td>', f'<tr> <td><a href=".*?">{t}</a></td>', s1)
s1 = s0
source = source s1
driver.close()
return source
def send_email():
"""
发送test_report邮件
"""
# 获取页面源码
source_code = get_report_source_code()
# 以126邮箱为例
# ----------------发件相关参数----------------
smtpserver = 'smtp.126.com'
port = 0
sender = 'testops_jianshuai@126.com'
password = 'xxxxxxxxxxxxxxx'
receicer = ['gaojs@xxxxxxs.com.cn', '13152020000@163.com']
# ----------------编辑邮件内容----------------
subject = '204 server 最新builds 一览表'
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'./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('******************* 邮件发送完成,请查收附件! ************************')
def download():
"""
自动下载release
"""
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Firefox(options=chrome_options)
print('*************************** 正在刷新新版本,请稍等 *******************************')
url = 'http://10.3.0.20/sp2/build/rel_ag_9_4.html'
time.sleep(5)
driver.get(url)
driver.maximize_window()
print('downloading')
start_time = time.time()
try:
element = driver.find_element_by_xpath(xpath='/html/body/blockquote[1]/a[1]').text
release_url = element.split(' ')[0]
print(release_url)
# 分离url和文件名称
release_name = os.path.split(release_url)[1]
print(release_name)
finally:
os.chdir('/var/www/html/builds/AG9.4/')
if not os.path.exists('/var/www/html/builds/AG9.4/' release_name):
os.system(r'curl -o %s %s' % (release_name, release_url))
end_time = time.time()
driver.close()
print(f'*************************** 总共用时 {end_time - start_time} s **************************')
# 断开vpn连接
if __name__ == '__main__':
send_email()
结果: