python3 网站状态监控_用python实现监控网站状态,并发送告警邮件

#!/usr/bin/env python

#coding:utf8

import os

import time

import smtplib

from email.mime.text import MIMEText

from email import Utils

import socket

def sendmail(to,subject,content):

msg = MIMEText(content)

lable_pwd="password"

msg['from'] = '****@sina.com'

msg['to'] = to

msg['subject'] = subject

msg['date'] = Utils.formatdate(localtime=1)

msg['message-id'] = Utils.make_msgid()

try:

smtpObj = smtplib.SMTP()

smtpObj.connect("smtp.sina.com:25")

try:

smtpObj.login("****@sina.com",lable_pwd)

me = "****@sina.com"

smtpObj.sendmail(me,to,msg.as_string())

print "Congratulations !Your mail have been sended Success !"

except smtplib.SMTPAuthenticationError,smtplib.SMTPException:

print "Login failed ,Please check the username/password."

finally:

try:

smtpObj.close()

except smtplib.SMTPException:

pass

except smtplib.SMTPException,e:

print "Error: unable to send email %s" % e

def check_server(f):

try:

fo = open(f)

for line in fo:

now = str(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

line = line.rstrip('\n')

print '正在检测%s的80端口是否正常 %s:......' % (line,now)

try:

s.connect((line,80))

print "地址%s检测正常" % line

except socket.error,e:

print "地址%s检测挂啦" % line

sc = 'The web server %s is down '% line

cc = 'The web server %s is down at %s' % (line,now)

sendmail('****@****.com',sc,cc)

finally:

s.close()

except IOError,e:

print "error: %s" % e

finally:

try:

fo.close()

except IOError,e:

print "error : %s" % e

if __name__=='__main__':

check_server('D:/workspace/studd/company_list')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值