- domains: A list of domains without protocol or any slashes. Example:
["google.com", "amazon.com"]
Created
August 12, 2023 01:16
-
-
Save PythonCoderAS/322b849d02c15e6187a6b6f8c74c5dc3 to your computer and use it in GitHub Desktop.
How to mass resolve a domain list to find valid domains
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
import asyncio | |
async def attempt_connection_domain(host) -> tuple[bool, bool]: # returns: [successful, https_supported] | |
try: | |
async with asyncio.timeout(5): | |
await asyncio.open_connection(host, 443) | |
return True, True | |
except (asyncio.TimeoutError, OSError): | |
try: | |
async with asyncio.timeout(5): | |
await asyncio.open_connection(host, 80) | |
return True, False | |
except (asyncio.TimeoutError, OSError): | |
return False, False | |
async def try_all_domain(domain): | |
success = https = False | |
success, https = await attempt_connection_domain(domain) | |
if not success: | |
if domain.startswith('www.'): | |
return None | |
else: | |
return await try_all_domain('www.' + domain) | |
front = "http" | |
if https: | |
front += "s" | |
return f"{front}://{domain}" | |
async def run_domains(): | |
return dict(zip(domains, await asyncio.gather(*map(lambda domain: try_all_domain(domain), domains)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment