Skip to content

Instantly share code, notes, and snippets.

@kitelife
Created August 8, 2012 10:17
Show Gist options
  • Save kitelife/3294051 to your computer and use it in GitHub Desktop.
Save kitelife/3294051 to your computer and use it in GitHub Desktop.
Check some website(s) regularly
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import requests
import sched
import time
import smtplib
from email.mime.text import MIMEText
from email.header import Header
websites = {'http://www.test.com': {'failureCount':0, 'needSendMail': True}}
MAILHOST = 'smtp.gmail.com:587'
MAILFROM = "xxx@gmail.com"
MAILPASSWORD = 'xxx'
MAILTO = 'xxx@xxx'
SUBJECT = "服务器可能无法正常访问了"
def main():
s = sched.scheduler(time.time, time.sleep)
while 1:
for url in websites.iterkeys():
s.enter(5, 1, checkWebsite, (url,))
s.run()
for (website, status) in websites.iteritems():
#print website, status['failureCount'], status['needSendMail']
if status['failureCount'] == 5 and status['needSendMail']:
#print 'failure'
sendGmail(website + ' ===> ' '可能无法正常访问了,请赶紧检查')
websites[website]['failureCount'] = 0
websites[website]['needSendMail'] = False
#elif status['failureCount'] == 0:
# print website, '===>', 'access successfully'
def checkWebsite(url):
try:
response = requests.head(url)
if response.status_code == 200:
websites[url]['failureCount'] = 0
websites[url]['needSendMail'] = True
else:
websites[url]['failureCount'] += 1
except Exception, e:
#print e.message
websites[url]['failureCount'] += 1
def sendGmail(msg):
mailto = MAILTO
mailfrom = MAILFROM
mailpassword = MAILPASSWORD
mailhost = MAILHOST
subject = SUBJECT
email = MIMEText(msg, 'text', 'utf-8')
email['Subject'] = Header(subject, 'utf-8')
email['From'] = Header(mailfrom, 'utf-8')
email['To'] = mailto
try:
s = smtplib.SMTP(mailhost)
s.ehlo()
s.starttls()
s.ehlo()
s.login(mailfrom, mailpassword)
s.sendmail(mailfrom, mailto, email.as_string())
s.quit()
except Exception, e:
print str(e)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment