python监控网站运行异常并发送邮件的方法


Posted in Python onMarch 13, 2015

本文实例讲述了python监控网站运行异常并发送邮件的方法。分享给大家供大家参考。具体如下:

这是一个简单的python开发的监控程序,当指定网页状态不正常是通过smtp发送通知邮件

#!/usr/bin/env python

# -*- coding: UTF-8 -*-

#author  libertyspy

import socket

import smtplib

import urllib

mail_options = {

    'server':'smtp.qq.com',#使用了QQ的SMTP服务,需要在邮箱中设置开启SMTP服务

    'port':25,             #端口

    'user':'hacker@qq.com',#发送人

    'pwd':'hacker',        #发送人的密码

    'send_to':'sniper@qq.com',  #收件者

}

msg_options={

    'user':'hacker',    #短信平台的用户名

    'pwd':'74110',      #短信平台的密码

    'phone':'12345678910',   #需要发短信的电话号码

}

test_host = 'http://www.lastme.com/'

def url_request(host,port=80):

    try:

        response = urllib.urlopen(host)

        response_code = response.getcode()

        if 200 != response_code:

            return response_code

        else:

            return True

    except IOError,e:

        return False

def send_message(msg,host,status):

    send_msg='服务器:%s挂了!状态码:%s' % (host,status)

    request_api="http://www.uoleem.com.cn/api/uoleemApi?username=%s&pwd=%s&mobile=%s&content=%s"  \

            % (msg['user'],msg['pwd'],msg['phone'],send_msg)

    return url_request(request_api)

def send_email(mail,host,status):

    smtp = smtplib.SMTP()

    smtp.connect(mail['server'], mail['port'])

    smtp.login(mail['user'],mail['pwd'])

    msg="From:%s\rTo:%s\rSubject:服务器: %s 挂了 !状态码:%s\r\n" \

         % (mail['user'],mail['send_to'],host,status)

    smtp.sendmail(mail['user'],mail['send_to'], msg)

    smtp.quit()

"""

def check_status(host,port=80):

    s = socket.socket()

    ret_msg = []

    try:

        s.connect((host,port))

        return True

    except socket.error,e:

        return False

"""

if __name__=='__main__':

    status = url_request(test_host)

    if status is not True and status is not None:

        send_email(mail_options,test_host,status)

        send_message(msg_options,test_host,status)

    else:

        pass

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
Python中zip()函数用法实例教程
Jul 31 Python
在Python的web框架中编写创建日志的程序的教程
Apr 30 Python
python利用正则表达式排除集合中字符的功能示例
Oct 10 Python
Python求出0~100以内的所有素数
Jan 23 Python
python SocketServer源码深入解读
Sep 17 Python
python tkinter之 复选、文本、下拉的实现
Mar 04 Python
Python如何存储数据到json文件
Mar 09 Python
python爬虫开发之PyQuery模块详细使用方法与实例全解
Mar 09 Python
keras打印loss对权重的导数方式
Jun 10 Python
pytorch  网络参数 weight bias 初始化详解
Jun 24 Python
在python中对于bool布尔值的取反操作
Dec 11 Python
教你怎么用Python selenium操作浏览器对象的基础API
Jun 23 Python
go语言计算两个时间的时间差方法
Mar 13 #Python
python实现端口转发器的方法
Mar 13 #Python
python实现超简单端口转发的方法
Mar 13 #Python
python简单程序读取串口信息的方法
Mar 13 #Python
python通过BF算法实现关键词匹配的方法
Mar 13 #Python
python通过装饰器检查函数参数数据类型的方法
Mar 13 #Python
python实现简单温度转换的方法
Mar 13 #Python
You might like
PHP合并数组+号和array_merge的区别
2015/06/25 PHP
Linux环境下php实现给网站截图的方法
2016/05/03 PHP
Laravel框架基于ajax实现二级联动功能示例
2019/01/17 PHP
PHP操作XML中XPath的应用示例
2019/07/04 PHP
javascript计算当月剩余天数(天数计算器)示例代码
2014/01/09 Javascript
动态的绑定事件addEventListener方法的使用
2014/01/24 Javascript
基于javascript的JSON格式页面展示美化方法
2014/07/02 Javascript
javascript实现 百度翻译 可折叠的分享按钮列表
2015/03/12 Javascript
javascript函数式编程程序员的工具集
2015/10/11 Javascript
基于Jquery和html5的7款个性化地图插件
2015/11/17 Javascript
动态加载JavaScript文件的两种方法
2016/04/22 Javascript
ajax +NodeJS 实现图片上传实例
2017/06/06 NodeJs
React Native 搭建开发环境的方法步骤
2017/10/30 Javascript
深入理解vue-class-component源码阅读
2019/02/18 Javascript
详解nuxt 微信公众号支付遇到的问题与解决
2019/08/26 Javascript
JS实现多功能计算器
2020/10/28 Javascript
[02:09]抵达西雅图!中国军团加油!
2014/07/07 DOTA
Python实现登录人人网并抓取新鲜事的方法
2015/05/11 Python
解决Django的request.POST获取不到内容的问题
2018/05/28 Python
python 用正则表达式筛选文本信息的实例
2018/06/05 Python
如何在Django中设置定时任务的方法示例
2019/01/18 Python
Python中三元表达式的几种写法介绍
2019/03/04 Python
手把手教你pycharm专业版安装破解教程(linux版)
2019/09/26 Python
SELENIUM自动化模拟键盘快捷键操作实现解析
2019/10/28 Python
利用Python绘制Jazz网络图的例子
2019/11/21 Python
python实现全排列代码(回溯、深度优先搜索)
2020/02/26 Python
简单了解Java Netty Reactor三种线程模型
2020/04/26 Python
工程师必须了解的LRU缓存淘汰算法以及python实现过程
2020/10/15 Python
杠杆的科学教学反思
2014/01/10 职场文书
挖掘机司机岗位职责
2014/02/12 职场文书
创业融资计划书
2014/04/25 职场文书
奥巴马的演讲稿
2014/05/15 职场文书
爱护公共设施演讲稿
2014/09/13 职场文书
2014年党员学习“三严三实”思想汇报
2014/09/15 职场文书
亮剑精神观后感
2015/06/05 职场文书
志愿者工作心得体会
2016/01/15 职场文书