最近公司erp服务器无规律、不间断、时不时抽风,往往都是挂了快个把小时后其它部门的人才打电话过来说服务器挂了。于是用python写了一个简单的网页监控。程序主要监控网页状态码,200为正常,否则视为服务器挂了。每隔70秒查询一次,若发现三次连续的查询中都报错误,则通过预先设定的邮箱发送警告邮件。邮件发送后隔30分钟再次监控设定网页。

verson 1

直接将日志直接通过屏幕输出

#coding:utf-8
#author:ID404
#python verson 2.7.9import smtplib
import urllib
import timedef sendmail():mail_to = smtplib.SMTP('smtp.126.com',25)         #设置邮件发送服务器mail_to.login("send_mail@126.com","123456")      #设置发送邮件的帐号,密码msg = """From: system <send_mail@126.com>
To: <receive_mail@126.com>
Subject: webserver_downweb server is down
"""mail_to.sendmail('send_mail@126.com','receive_mail@126.com',msg)mail_to.close()if __name__ == '__main__':    print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),'server monitor is running'while 1:count=0error_status_count=0while count<3:time.sleep(70)      #每隔70秒监控一次服务器try:status=urllib.urlopen("http://192.168.0.8").code   #收集监控网址的网页状态码if status==200:print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),'web server is functional'if status<>200:error_status_count+=1  print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),'web servier is down ,error status count:',error_status_count,'status number',statusexcept:error_status_count+=1    #网页状态错误次数进行累加print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),'web servier is down ,error status count:',error_status_countcount+=1if error_status_count>=3: #网页状态错误超过3次自动发送报警邮件print 'error status count is :',error_status_count,'sending email,the program wiil try to monint the server after half an hour'sendmail()time.sleep(1800)      #邮件发送后半小时再后再次监控网页

verson 2

日志将在同目录下生成logs.txt

#coding:utf-8
#author:ID404
#python verson 2.7.9
#程序主要监控网页状态码,200为正常,否则视为服务器挂了。每隔70秒查询一次,若发现三次连续的查询中都报错误,则通过预先设定的邮箱发送警告邮件。邮件发送后隔30分钟再次监控设定网页。import smtplib
import urllib
from datetime import *
import timefrom tkMessageBox import *

def sendmail():

send_mail="send_system@126.com"
    send_mail_passwd="123456"
    receive_mail='rec@126.com'
    send_mail_server='smtp.126.com'

mail_to = smtplib.SMTP(send_mail_server,25)
    mail_to.login(send_mail,send_mail_passwd)
    msg ="From: send_system <"+send_mail+""">
To: <"""+receive_mail+""">
Subject: web server is down

web server is down
"""
    mail_to.sendmail(send_mail,receive_mail,msg)
    mail_to.close()

if __name__ == '__main__':logs=open('./logs.txt','a+')logs.writelines(['\n',str(datetime.now()),'    the program is running'])logs.close()while 1:count=0error_status_count=0while count<3:time.sleep(70)      #每隔70秒监控一次服务器try:status=urllib.urlopen("http://192.168.0.8").code   #收集监控网址的网页状态码if status==200:logs=open('./logs.txt','a+')logs.writelines(['\n',str(datetime.now()),'    web server is functional'])logs.close()                    if status<>200:error_status_count+=1logs=open('./logs.txt','a+')logs.writelines(['\n',str(datetime.now()),'    web servier is down ,error status count:',str(error_status_count),'    status number:',str(status)])logs.close()except:error_status_count+=1    #网页状态错误次数进行累加logs=open('./logs.txt','a+')logs.writelines(['\n',str(datetime.now()),'    web servier is down ,error status count:',str(error_status_count)])logs.close()count+=1if error_status_count>=3: #网页状态错误超过3次自动发送报警邮件logs=open('./logs.txt','a+')logs.writelines(['\n','error status count is :',str(error_status_count),'    sending email,the program wiil try to monint the server after half an hour'])logs.close()            showwarning('attention',['tzx webserver is down!',str(datetime.now())])sendmail()time.sleep(1800)      #邮件发送后半小时再后再次监控网页

转载于:https://www.cnblogs.com/id404/p/4345059.html

python监控网页状态相关推荐

  1. python监控网页更新_【小白教程】Python3监控网页

    之前用RSS来监控网页更新内容,可惜刷新时间太长了,三个小时..只能看看新闻啥的,又没有小钱钱充会员(摊手 听说Python可以做这个功能,抱着试试看的态度,本以为会很麻烦,没想到这么简单哈哈~我从来 ...

  2. python 监控网页_python实时监控网页

    PHP 从语言层面上讲几乎是一无是处,具体实现的质量也乏善可陈,但它胜在最要命的部署上:没有任何其他语言有像 PHP 一样适合大规模部署的方式.基本上装好 Apache/mod_php 之后,PHP ...

  3. python监控网页更新_python监控网页更新

    {"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],&q ...

  4. python监控网页_python实时监控网页

    {"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],&q ...

  5. python脚本监控网站状态 - 赵海华_运维之路 - 51CTO技术博客

    python脚本监控网站状态 - 赵海华_运维之路 - 51CTO技术博客 python脚本监控网站状态 2013-01-09 09:21:02 标签:监控 python 原创作品,允许转载,转载时请 ...

  6. python模拟点击后获取状态码_Python获取网页状态码

    在网页爬取的时候,有时候需要判断下要爬取网页的网页HTTP状态码,然后再进行相关的工作. 1.网页HTTP状态码 HTTP状态码表示HTTP协议所返回的响应状态.具体参考:http://baike.b ...

  7. python监控当前联网状态详情

    介绍一个利用Python监控当前联网状态情况的python代码,它可以清楚地知道,你的电脑网络是否是链接成功或失败,通俗的说,就是查看你的电脑有木有网,代码如下: 调用系统网络诊断 监控网络是否链接成 ...

  8. python爬虫 - GET和POST分别对爬取的网页状态的影响以及常见网页状态码(200、401、402、404等等)

         主要内容:GET和POST分别对爬取的网页状态的影响以及常见的网页状态码 目录 区分网页请求GET or POST get方法的特点 post方法的特点 网页返回状态码 200 非200 区 ...

  9. python3刷新监控网页_python - 如何不刷新网页而监控网页变化?

    问 题 我在用python监控一个网页 这个网页不定时的会更新 我要寻找需要匹配的关键词比如'ABC' 大概的程序框架如下 基本方法就是 用 selenium 获取源码 然后beautifulsoup ...

最新文章

  1. 机器学习数据预处理之缺失值:插值法填充+多项式插值
  2. js从0开始构思表情插件
  3. Java Spring MVC项目搭建(三)——“Hello World”
  4. 简述linux各个组成部分的定义及功能,Mariadb的架构及相关概念
  5. java servlet 返回图片_SpringMVC返回图片的几种方式
  6. tf.where 用法
  7. 轻芒阅读距离今日头条还差一个即刻
  8. 海龟交易法则01_玩风险的交易者
  9. 如何使用IDEA运行Spring实战(第四版)代码
  10. cad画计算机网络图,CAD迷你家装五步快速绘制平面图教程
  11. 计算机在录制声音过程中流向,Bandicam中录制电脑声音的具体流程介绍
  12. html5的header元素作用,html5header标签怎么用?html5header标签的作用介绍-
  13. 无线网络技术:GPS(美国全球定位系统)的发展史
  14. word2007使用笔记 - 标题的分级编号
  15. 服务器宕机指的是什么意思?
  16. 小书MybatisPlus第9篇-常用字段默认值自动填充
  17. Gtarcade的Hunger Heroes游戏马拉松即将开始
  18. 浏览器获取图片出现跨域问题
  19. android system image 编译出错,Android framework 编译常见错误
  20. B站不能拖动视频进度条问题

热门文章

  1. MySQL优化--索引
  2. 云计算具有什么平台_工业云平台具有哪些功能?
  3. Python 语言程序设计(2)基本图形绘制
  4. php数量百分比,mysql PHP行数+百分比
  5. r语言的rmd文件转换成html,.Rmd文件转化为PDF报告
  6. 北京理工大学计算机学院杨晨,杨旭_北京理工大学计算机学院
  7. 系统学习NLP(二十二)--主题模型LDA
  8. 前景检测算法(十七)--基于光流算法
  9. OpenCV之图像平滑处理
  10. redis scala编程之创建连接