python监控网站运行异常并发送邮件详解编程语言
2023-06-13 09:20:24 时间
一个简单的python开发的监控程序,当指定网页状态不正常是通过smtp发送通知邮件
#!/usr/bin/env python # -*- coding: UTF-8 -*- #author libertyspy #link http://www.lastme.com import socket import smtplib import urllib mail_options = { server:smtp.qq.com,#使用了QQ的SMTP服务,需要在邮箱中设置开启SMTP服务 port:25, #端口 user:[email protected],#发送人 pwd:hacker, #发送人的密码 send_to:[email protected], #收件者 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
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/8433.html
cjavapython相关文章
- 通过pycharm安装python_pycharm编译器安装教程
- Python获取时间戳_python爬虫时间戳
- python监控网页内容变化_使用Python监控文件内容变化代码实例
- python最好的开发工具_Python编译器
- Python编程 变量与常量
- 树莓派3B+ python 实时监控(USB摄像头)并显示
- 【说站】python not关键字的使用
- 【说站】python中mock有哪些统计的方法
- 【说站】python poetry创建虚拟环境
- Python修改文件后缀_python重命名文件名
- python querystring encode_querystring
- python语言关键字是_Python 关键字
- python抛出异常和捕获异常_Python异常
- Python自动化之Python列表
- python递归函数讲解_Python递归函数实例讲解
- 软件测试|教你用Python给领导发邮件
- 1行Python代码,把PPT转成图片,python-office功能更新~
- Python基础-11 标准库简介(二)
- python教程:shutil高级文件操作
- Python基础语法-函数-定义函数
- Python index()方法:检测字符串中是否包含某子串
- Python函数默认参数设置(超级详细)
- 使用Python编程连接MySQL数据库(python连mysql)
- 使用Python连接SQL Server数据库(python连接sqlserver数据库)
- 使用这个开源工具来监控 Python 中的变量
- python驱动使用pip安装MySQL Python驱动的简单步骤(pip安装mysql)
- 如何在Linux中安装Python?(linux安装python)
- 从 Python 连接到 MySQL:实现更多强大的数据库应用(python和mysql)
- 在Linux系统中使用Python编程语言(linux中python)
- python教程之用py2exe将PY文件转成EXE文件