2019独角兽企业重金招聘Python工程师标准>>>
cat common.py(该文件为一个公共库文件;实现了两个功能:视图截取、邮件发送)
#! /usr/bin/env python
#coding=utf-8
import time,os
import urllib
import urllib2
import cookielib
import MySQLdb
import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.header import Header
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.proxy import ProxyType
def take_screenshot(url, save_fn):
headers = {'Authorization':'Bearer eyJrIjoiOWNCZW9lRnpBRGZzenhSSlA3QzJXU3hTU0lWZERsMmYiLCJuIjoiZ3JhZmFuYSByZXBvcnQiLCJpZCI6MX0='}
desire = DesiredCapabilities.PHANTOMJS.copy()
for key, value in headers.iteritems():
desire['phantomjs.page.customHeaders.{}'.format(key)] = value
driver = webdriver.PhantomJS(executable_path='/usr/local/bin/phantomjs',desired_capabilities=desire, service_args=['--load-images=yes'])
#phantomjs截取整张网页
# driver = webdriver.PhantomJS()
driver.set_window_size(1920, 2000)
driver.get(url) # Load page
#将页面的滚动条拖到最下方,然后再拖回顶部
driver.execute_script("""
(function () {
var y = 0;
var step = 100;
window.scroll(0, 0);
function f() {
if (y < document.body.scrollHeight) {
y += step;
window.scroll(0, y);
setTimeout(f, 100);
} else {
window.scroll(0, 0);
document.title += "scroll-done";
}
}
setTimeout(f, 1000);
})();
""")
for i in xrange(30):
if "scroll-done" in driver.title:
break
time.sleep(10)
driver.save_screenshot(save_fn)
driver.close()
def send_mail(username,password,smtp_server,smtp_port,to_list,sub,image):
msgRoot = MIMEMultipart('related')
me = "ops"+"<"+username+">"
msgRoot["Subject"] = sub
msgRoot["From"] = Header("ops", 'utf-8')
msgRoot["To"] = ";".join(to_list)
msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)
mail_msg = """
"""
msgAlternative.attach(MIMEText(mail_msg, 'html', 'utf-8'))
print time.strftime('%Y-%m-%d %H:%M',time.localtime(time.time()))
print to_list
fp = open(image,'rb')
msgImage = MIMEImage(fp.read())
fp.close()
# 定义图片ID
msgImage.add_header('Content-ID', '')
msgRoot.attach(msgImage)
print smtp_server
try:
server = smtplib.SMTP()
server.connect(smtp_server,smtp_port)
#server.ehlo()
#server.starttls()
server.login(username,password
server.sendmail(me,to_list,msgRoot.as_string())
server.close()
print "邮件发送成功"
except smtplib.SMTPException,e:
print "Error: 无法发送邮件"
print e
具体报表文件
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import time
import common
# mail
sub = "ELK平台技术运营日报"
smtp_server = "smtp.web.com"
smtp_port = "443"
mail_user = "user"
mail_pass = "pass"
domain = ''
BOSS_team = ["[email protected]"]
NORMAL_team = ["[email protected]"]
to_list = BOSS_team + NORMAL_team
#png
timestr = time.strftime("%Y%m%d", time.localtime())
png_file = "ELK_Daily"+timestr+".png"
if __name__ == "__main__":
common.take_screenshot("http://ip:3000/d/0ZdLbJliz/es-ls?orgId=1&from=now-1d%2Fd&to=now-1d%2Fd&kiosk=tv",png_file)
common.send_mail(mail_user,mail_pass,smtp_server,smtp_port,to_list,sub,png_file)
os.popen("mv *png pngback ; cat ghostdriver.log >> logs/ghostdriver.log ; rm ghostdriver.log")