From 172bccd05779240d6d6cd087a1c19216f0a5e225 Mon Sep 17 00:00:00 2001 From: "Brandon C. Irizarry" Date: Thu, 16 Apr 2026 16:34:00 -0400 Subject: feat: ping sites sequentially and measure how long it takes --- main.py | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'main.py') diff --git a/main.py b/main.py index 463b5df..80dcce7 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,9 @@ +import asyncio import csv import sys +import time + +import aiohttp def get_urls(filename: str, limit: int | None = None) -> list[str]: @@ -29,6 +33,32 @@ def get_urls(filename: str, limit: int | None = None) -> list[str]: return urls +async def ping(urls: list[str], max_concurrency=None) -> None: + """Make a GET request to members of URLS. + + If MAX_CONCURRENCY is None, browse every site at once. + + Else, only browse MAX_CONCURRENCY number of sites at a time. + + Print the sites as they get browsed; don't return anything. + + """ + + async with aiohttp.ClientSession(max_field_size=8190 * 2) as session: + for url in urls: + try: + async with session.get(url) as response: + print(f"Status: {response.status}") + + if not response.ok: + print(f"Got code {response.status} from URL; skipping") + continue + + print(f"Content-Type: {response.headers['content-type']}") + except aiohttp.ClientError as e: + print(f"Something bad happened with URL: {e}; skipping") + + def main(): limit: int | None = None @@ -36,7 +66,13 @@ def main(): limit = int(sys.argv[1]) urls = get_urls("majestic_million.csv", limit) - print(urls) + + start_time = time.perf_counter() + asyncio.run(ping(urls)) + end_time = time.perf_counter() + + elapsed_time = end_time - start_time + print(f"Execution time: {elapsed_time:.6f} seconds") if __name__ == "__main__": -- cgit v1.2.3