Skip to content

Instantly share code, notes, and snippets.

@PythonCoderAS
Created August 12, 2023 01:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PythonCoderAS/322b849d02c15e6187a6b6f8c74c5dc3 to your computer and use it in GitHub Desktop.
Save PythonCoderAS/322b849d02c15e6187a6b6f8c74c5dc3 to your computer and use it in GitHub Desktop.
How to mass resolve a domain list to find valid domains

Input

  • domains: A list of domains without protocol or any slashes. Example: ["google.com", "amazon.com"]
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