├── .gitignore ├── README.md ├── down ├── __init__.py └── down ├── requirements.txt └── setup.py /.gitignore: -------------------------------------------------------------------------------- 1 | dist 2 | *.egg-info 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # down 2 | > Check if a website is up or down. 3 | 4 |

5 | 6 | 7 | 8 |

9 | 10 | 11 | # Installation 12 | 13 | ```bash 14 | $ pip install --user down 15 | ``` 16 | 17 | ## Usage 18 | ```bash 19 | Usage: down [file] [url] 20 | 21 | Example 22 | down url_list.txt 23 | down https://www.example.com 24 | ``` 25 | -------------------------------------------------------------------------------- /down/__init__.py: -------------------------------------------------------------------------------- 1 | name = "down" 2 | -------------------------------------------------------------------------------- /down/down: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | import sys 4 | import requests 5 | import asyncio 6 | import aiohttp 7 | 8 | good = "\033[92m✔\033[0m" 9 | bad = "\033[91m✘\033[0m" 10 | 11 | # Header is needed for some sites or else they will think 12 | # that a bot is accessing the site wont return 200 13 | headers = { 14 | 'User-Agent': 15 | 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:55.0) ' 16 | 'Gecko/20100101 Firefox/55.0' 17 | } 18 | 19 | 20 | def show_help(): 21 | """ 22 | Show the help message 23 | """ 24 | 25 | help_message = """ 26 | Usage: python3 down.py [file] [url] 27 | 28 | Example 29 | python3 down.py url_list.txt 30 | python3 down.py https://www.example.com 31 | """ 32 | 33 | sys.stdout.write(help_message) 34 | sys.exit() 35 | 36 | 37 | def print_status(up, site): 38 | """ 39 | Print the status of the site to stdout 40 | :param up: Is the site up? True or False 41 | :type up: bool 42 | :param site: The site URL 43 | :type site: str 44 | """ 45 | 46 | sys.stdout.write("{} {}\n".format(good if up else bad, site)) 47 | 48 | 49 | def _url(site): 50 | """ 51 | Check if site is up 52 | :param site: The url of the site 53 | :type site: str 54 | """ 55 | try: 56 | r = requests.get(site) 57 | if r.status_code != 200: 58 | print_status(False, site) 59 | else: 60 | print_status(True, site) 61 | except requests.ConnectionError: 62 | print_status(False, site) 63 | 64 | 65 | async def _get(url, session): 66 | """ 67 | Async check of site status 68 | :param site: The site that will be checked 69 | :param session: The instance the check is made with 70 | :type site: str 71 | :type session: obj 72 | """ 73 | try: 74 | async with session.get(url=url) as response: 75 | assert response.status == 200, print_status(False, url) 76 | print_status(True, url) 77 | except Exception: 78 | print_status(False, url) 79 | 80 | 81 | async def main(sites): 82 | """ 83 | Performs async up-status check of sites list 84 | :param sites: List of site urls 85 | :type sites: list of str 86 | """ 87 | async with aiohttp.ClientSession() as session: 88 | await asyncio.gather(*[_get(site, session) for site in sites]) 89 | 90 | 91 | 92 | if len(sys.argv) == 1 or sys.argv[1] == "-h": 93 | show_help() 94 | 95 | # Checking if url or file 96 | if sys.argv[1].startswith("http"): 97 | _url(sys.argv[1]) 98 | sys.exit() 99 | 100 | # Async check of urls in file 101 | with open(sys.argv[1], 'r') as file: 102 | try: 103 | sites = [site.rstrip('\n') for site in file] 104 | except FileNotFoundError: 105 | sys.stdout.write("No such file: {}".format(file)) 106 | 107 | asyncio.run(main(sites)) 108 | sys.exit() 109 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | requests 2 | asyncio 3 | aiohttp -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import setuptools 2 | 3 | with open('README.md', 'r') as fh: 4 | long_description = fh.read() 5 | 6 | setuptools.setup( 7 | name='down', 8 | version='1.0', 9 | author='Siddharth Dushantha', 10 | author_email='siddharth.dushantha@gmail.com', 11 | description='A CLI tool to check if a site or a list of sites are down or up', 12 | long_description=long_description, 13 | long_description_content_type='text/markdown', 14 | url='https://github.com/sdushantha/down', 15 | packages=setuptools.find_packages(), 16 | scripts=['down/down'], 17 | install_requires=['requests'] 18 | ) 19 | --------------------------------------------------------------------------------