#python2.7下编写。使用python abc.py py2exe 转成exe
#所以需要安装py2exe包
#filename:webmonitor.py
import os
import sys
import pycurl
import StringIO
import json
import time
import smtplib
import ConfigParser
from email.mime.text import MIMEText
from email.header import Header
#print pycurl.version_info()
cur_dir = os.path.dirname(os.path.abspath(sys.argv[0])) + "\"
cfg = ConfigParser.ConfigParser()
cfg.readfp(open(cur_dir + ‘abc.ini‘))
email_from = cfg.get("mail","from")
email_pwd = cfg.get("mail","password") #邮箱的授权码,不是邮箱密码
email_to = cfg.get("mail","to")
email_host = cfg.get("mail","host")
email_port = cfg.get("mail","port")
email_url = cfg.get("mail","url")
#print email_from,email_pwd,email_to,email_host,email_port
#print url
#exit(-10)
dt = time.strftime(‘%Y-%m-%d %H:%M:%S‘)
#fn_path = os.path.dirname(os.path.abspath(sys.argv[0])) + "\"
fn_dt = cur_dir + "abcmonitor"+time.strftime(‘%Y%m%d‘)+".log"
#print dt,fn_dt
#time.sleep(15)
fp = open(fn_dt,"a+")
c = pycurl.Curl()
c.setopt(c.URL,email_url)
try:
c.perform()
except Exception as e:
print("exception error :[%s]"%(e))
fp.write("%s:%s\r\n"%(dt,e))
finally:
c.close
ret = c.getinfo(c.HTTP_CODE)
#print("ret= %s" %(ret))
if ret == 200:
subject = ‘web ok‘
else:
subject = ‘web error‘
#print("subject="+subject)
fp.write("%s:%s\r\n"%(dt,subject))
mail_host = email_host
mail_user = email_from
mail_pass = email_pwd
sender = email_from
receivers = email_to
#print receivers
#exit(-1000)
message = MIMEText(‘abc.cn monitor‘)
message[‘From‘] = Header(email_from,‘utf-8‘)
message[‘To‘] = Header( email_to,‘utf-8‘)
message[‘subject‘] = Header(subject,‘utf-8‘)
try:
smtpobj = smtplib.SMTP_SSL(email_host,email_port)
smtpobj.login(mail_user,mail_pass)
smtpobj.sendmail(sender,receivers.split(","),message.as_string())
#print "mail ok"
except smtplib.SMTPException as em:
fp.write("MailSendError:%s"%(em))
#print "Error:mail fail"
finally:
smtpobj.quit()
c.close()
fp.close()
#以下是要转换成exe时,单独写一个py abc.py
from distutils.core import setup
import py2exe
import sys
setup(console=["webmonitor.py"])
配置文件一定要放到exe生成后的dist目录中。ini的格式如下br/>[mail]
[email protected]
password=abcbr/>[email protected],[email protected]
host=smtp.qq.com
port=465
url=http://www.abc.cn
原文地址:https://blog.51cto.com/weiweiyong/2391433