python監(jiān)控網(wǎng)站運行異常并發(fā)送郵件的方法
更新時間:2015年03月13日 15:37:26 作者:chongq
這篇文章主要介紹了python監(jiān)控網(wǎng)站運行異常并發(fā)送郵件的方法,涉及Python操作郵件及服務(wù)器監(jiān)控的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了python監(jiān)控網(wǎng)站運行異常并發(fā)送郵件的方法。分享給大家供大家參考。具體如下:
這是一個簡單的python開發(fā)的監(jiān)控程序,當指定網(wǎng)頁狀態(tài)不正常是通過smtp發(fā)送通知郵件
復制代碼 代碼如下:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#author libertyspy
import socket
import smtplib
import urllib
mail_options = {
'server':'smtp.qq.com',#使用了QQ的SMTP服務(wù),需要在郵箱中設(shè)置開啟SMTP服務(wù)
'port':25, #端口
'user':'hacker@qq.com',#發(fā)送人
'pwd':'hacker', #發(fā)送人的密碼
'send_to':'sniper@qq.com', #收件者
}
msg_options={
'user':'hacker', #短信平臺的用戶名
'pwd':'74110', #短信平臺的密碼
'phone':'12345678910', #需要發(fā)短信的電話號碼
}
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='服務(wù)器:%s掛了!狀態(tài)碼:%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:服務(wù)器: %s 掛了 !狀態(tài)碼:%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
# -*- coding: UTF-8 -*-
#author libertyspy
import socket
import smtplib
import urllib
mail_options = {
'server':'smtp.qq.com',#使用了QQ的SMTP服務(wù),需要在郵箱中設(shè)置開啟SMTP服務(wù)
'port':25, #端口
'user':'hacker@qq.com',#發(fā)送人
'pwd':'hacker', #發(fā)送人的密碼
'send_to':'sniper@qq.com', #收件者
}
msg_options={
'user':'hacker', #短信平臺的用戶名
'pwd':'74110', #短信平臺的密碼
'phone':'12345678910', #需要發(fā)短信的電話號碼
}
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='服務(wù)器:%s掛了!狀態(tài)碼:%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:服務(wù)器: %s 掛了 !狀態(tài)碼:%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程序設(shè)計有所幫助。
相關(guān)文章
Python的dict字典結(jié)構(gòu)操作方法學習筆記
這篇文章主要介紹了Python的dict字典結(jié)構(gòu)操作方法學習筆記本,字典的操作是Python入門學習中的基礎(chǔ)知識,需要的朋友可以參考下2016-05-05
K最近鄰算法(KNN)---sklearn+python實現(xiàn)方式
今天小編就為大家分享一篇K最近鄰算法(KNN)---sklearn+python實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
Python中set與frozenset方法和區(qū)別詳解
這篇文章主要介紹了Python中set與frozenset方法和區(qū)別詳解的相關(guān)資料,需要的朋友可以參考下2016-05-05
pycharm不在cmd中運行卻在python控制臺運行問題解決
這篇文章主要介紹了pycharm不在cmd中運行卻在python控制臺運行問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08

