python監控網站運行異常并發送郵件

n6xb 9年前發布 | 1K 次閱讀 Python

一個簡單的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':'hacker@qq.com',#發送人 'pwd':'hacker', #發送人的密碼 'send_to':'sniper@qq.com', #收件者 } msg_options={ 'user':'hacker', #短信平臺的用戶名 'pwd':'74110', #短信平臺的密碼 'phone':'12345678910', #需要發短信的電話號碼 } test_host = '

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</pre>

 本文由用戶 n6xb 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!