Skip to content

Instantly share code, notes, and snippets.

@Sulverus
Created September 13, 2013 07:21
Show Gist options
  • Save Sulverus/6547626 to your computer and use it in GitHub Desktop.
Save Sulverus/6547626 to your computer and use it in GitHub Desktop.
Simple http availability checker class =)
import requests
from datetime import datetime
HTTP_SUCCESS = 200
AGENT = 'CheckPy/0.0.1'
class HttpChecker():
"""
Simple http checker:
Usage:
sites = ['http://site1.com', 'http://site.ru', 'http://google.com']
c = HttpChecker(sites, verbose=True)
c.check()
"""
def __init__(self, url_list, verbose=False):
self. sites = url_list
self.verbose_mode = verbose
self.header = {'User-Agent': AGENT}
def check_url(self, url):
resp = requests.get(url, headers=self.header)
return resp.status_code
def format_point(self, point):
time = '%.2g sec' % point['time'] \
if point['response'] == HTTP_SUCCESS else None
info = '[%s]: %s %d by %s' % (
datetime.now().strftime('%m.%d.%Y %H:%M'),
point['url'],
point['response'],
time
)
return info
def check(self):
check_dict = {}
for url in self.sites:
start = datetime.now()
response = self.check_url(url)
end = datetime.now() - start
result = {
'url': url,
'response': response,
'time': end.total_seconds()
}
if self.verbose_mode:
print self.format_point(result)
if url not in check_dict:
check_dict[url] = []
check_dict[url].append(result)
return check_dict
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment