[Python] python监控网站运行异常并发送邮件 →→→→→进入此内容的聊天室

来自 , 2019-04-19, 写在 Python, 查看 115 次.
URL http://www.code666.cn/view/9d405c24
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. #author  libertyspy
  4. #link    http://www.lastme.com
  5.  
  6. import socket
  7. import smtplib
  8. import urllib
  9.  
  10. mail_options = {
  11.     'server':'smtp.qq.com',#使用了QQ的SMTP服务,需要在邮箱中设置开启SMTP服务
  12.     'port':25,             #端口
  13.     'user':'hacker@qq.com',#发送人
  14.     'pwd':'hacker',        #发送人的密码
  15.     'send_to':'sniper@qq.com',  #收件者
  16. }
  17. msg_options={
  18.     'user':'hacker',    #短信平台的用户名
  19.     'pwd':'74110',      #短信平台的密码
  20.     'phone':'12345678910',   #需要发短信的电话号码
  21. }
  22. test_host = 'http://www.lastme.com/'
  23. def url_request(host,port=80):
  24.     try:
  25.         response = urllib.urlopen(host)
  26.         response_code = response.getcode()
  27.         if 200 != response_code:
  28.             return response_code
  29.         else:
  30.             return True
  31.     except IOError,e:
  32.         return False
  33.  
  34. def send_message(msg,host,status):
  35.     send_msg='服务器:%s挂了!状态码:%s' % (host,status)
  36.     request_api="http://www.uoleem.com.cn/api/uoleemApi?username=%s&pwd=%s&mobile=%s&content=%s"  \
  37.             % (msg['user'],msg['pwd'],msg['phone'],send_msg)
  38.     return url_request(request_api)
  39.  
  40. def send_email(mail,host,status):
  41.     smtp = smtplib.SMTP()
  42.     smtp.connect(mail['server'], mail['port'])
  43.     smtp.login(mail['user'],mail['pwd'])
  44.     msg="From:%s\rTo:%s\rSubject:服务器: %s 挂了 !状态码:%s\r\n" \
  45.          % (mail['user'],mail['send_to'],host,status)
  46.     smtp.sendmail(mail['user'],mail['send_to'], msg)
  47.     smtp.quit()
  48. """
  49. def check_status(host,port=80):
  50.    s = socket.socket()
  51.    ret_msg = []
  52.    try:
  53.        s.connect((host,port))
  54.        return True
  55.    except socket.error,e:
  56.        return False
  57. """
  58. if __name__=='__main__':
  59.     status = url_request(test_host)
  60.     if status is not True and status is not None:
  61.         send_email(mail_options,test_host,status)
  62.         send_message(msg_options,test_host,status)
  63.     else:
  64.         pass
  65. #//python/8928

回复 "python监控网站运行异常并发送邮件"

这儿你可以回复上面这条便签

captcha