Created
November 9, 2021 19:51
-
-
Save chantra/db90d97ebe3936742158fb57b5dd3221 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import asyncio | |
import click | |
import aiohttp | |
import yaml | |
from functools import wraps | |
def coro(f): | |
@wraps(f) | |
def wrapper(*args, **kwargs): | |
return asyncio.run(f(*args, **kwargs)) | |
return wrapper | |
LETTERS = [chr(ord('A') + x) for x in range(13)] | |
URL_TPL = 'https://root-servers.org/root/{}/yaml' | |
async def get_root_server(session, letter, country, site_type): | |
url = URL_TPL.format(letter) | |
res = [] | |
async with session.get(url) as resp: | |
content = await resp.text() | |
sites = yaml.safe_load(content) | |
for site in sites['Sites']: | |
if site['Country'] in country and site['Type'] in site_type: | |
res.append('{} ({})'.format(site['Town'], site['Type'])) | |
return (letter, res) | |
@click.command() | |
@click.option("--country", default=[], multiple=True, help="2-letters country code.") | |
@click.option("--site-type", type=click.Choice(["Global", "Local"]), default=["Global"], multiple=True, help="Which type of entries to print out.") | |
@coro | |
async def main(country, site_type): | |
async with aiohttp.ClientSession() as session: | |
tasks = [] | |
for letter in LETTERS: | |
tasks.append(asyncio.ensure_future(get_root_server(session, letter, country, site_type))) | |
results = await asyncio.gather(*tasks) | |
for result in results: | |
if not len(result[1]): | |
continue | |
print(result[0] + ":") | |
for x in result[1]: | |
print("\t{}".format(x)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment