Skip to content

Instantly share code, notes, and snippets.

@Azlirn
Last active June 26, 2019 17:56
Show Gist options
  • Save Azlirn/36477e2dad9c60afa9a2a9a0cbbb2765 to your computer and use it in GitHub Desktop.
Save Azlirn/36477e2dad9c60afa9a2a9a0cbbb2765 to your computer and use it in GitHub Desktop.
A simple domain uptime checker
from threading import Thread
import requests
import time
with open('domains.txt') as f:
domainList = f.readlines()
domainList = [x.strip() for x in domainList]
#TODO: Check list for .onion domains and ignore said entries
# A temporary list used to store sites to be monitored when they are detected as "down"
sites_down = []
def site_up():
# Function to monitor "up" time
while True:
for domain in domainList:
try:
r = requests.get(domain)
if r.status_code == 200:
print('[* T1 *] %s --> HTTP 200 - URL OKAY' % str(domain))
else:
print('[! T1 !] %s --> HTTP STATUS: %s - Adding URL to "Down Checker""...' % (str(domain), str(r.status_code)))
if domain not in sites_down:
sites_down.append(domain)
except requests.ConnectionError:
print('[! T1 !] %s --> Connection Error - Ignoring This URL For Now...' % str(domain))
pass
print('[* T1 *] All domains checked - Sleeping for 15 minutes...\n')
time.sleep(900)
def site_down():
# Function to monitor "down" time
while True:
time.sleep(300)
for domain in sites_down:
try:
print('[! T2 !] Checking', domain, 'again...')
r = requests.get(domain)
if r.status_code == 200:
print('[* T2 *]', domain, '%s --> URL is now registering as ALIVE.\n')
sites_down.remove(domain)
else:
print('[! T2 !] HTTP Status %s - I will check this URL again shortly...\n' % str(r.status_code))
except requests.ConnectionError:
print('[! T2 !] Connection Error!')
pass
t1 = Thread(target=site_up)
t2 = Thread(target=site_down)
try:
print('*' * 75)
print('This script will check all URLs in domains.txt once every 15 minutes under Thread One (T1).')
print('If a connection error occurs, the script will ignore the URL and check it again once T1 completes, '
'and 15 minutes have elapsed.')
print('If any HTTP response other than \'200\' is returned, the script will add the URL to a \'DOWN\' checker '
'under Thread Two (T2)')
print('The down checker runs every 5 minutes - Once it detects a URL has become \'ALIVE\' once more, the URL is '
'removed from T2.')
print('*' * 75)
time.sleep(10)
print('\n[***] Starting Threads...\n')
t1.daemon = True
t2.daemon = True
t1.start()
t2.start()
while True:
time.sleep(100)
except (KeyboardInterrupt, SystemExit):
print('\n[!!!] Received Keyboard Interrupt - Quitting Threads...\n')
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment