Skip to content

Instantly share code, notes, and snippets.

@ikatson
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ikatson/4d0aa8295043879d0311 to your computer and use it in GitHub Desktop.
Save ikatson/4d0aa8295043879d0311 to your computer and use it in GitHub Desktop.
check that a site works, but do not fail from the first attempt
#!/usr/bin/env python
import argparse
import hashlib
import os
import traceback
import urllib2
import sys
def try_url(url, retries=1):
while retries:
retries -= 1
try:
urllib2.urlopen(url).read()
except Exception, e:
if not retries:
raise
else:
return
USAGE = """
check_url.py checks if a url works, and exist with 1, if it does not. However, the main
feature is checking multiple times so that a single failure does not count.
E.g.
check_url.py http://example.com/ --instant-retries 2 --deferred-retries 1
will try to get the url 2 times, and if it's not the first time the script is launched,
it will fail with an exception.
"""
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('url')
parser.add_argument('--instant-retries', type=int, default=2)
parser.add_argument('--deferred-retries', type=int, default=1)
parser.add_argument('--storage-dir', default='/tmp/.check_url/')
args = parser.parse_args()
if not os.path.exists(args.storage_dir):
os.makedirs(args.storage_dir)
attempts_path = os.path.join(args.storage_dir, 'attempts_%s' % hashlib.md5(args.url).hexdigest())
try:
with open(attempts_path) as f:
dattempts = int(f.read().strip())
except IOError:
dattempts = 0
# print args.url, args.instant_retries, args.deferred_retries
e = None
try:
try_url(args.url, args.instant_retries)
except Exception, e:
dattempts += 1
with open(attempts_path, 'w') as f:
f.write(str(dattempts))
if dattempts > args.deferred_retries:
traceback.print_exc()
sys.exit(1)
else:
try:
os.unlink(attempts_path)
except OSError:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment