resolve_ips_multi_async¶
-
privex.helpers.net.
resolve_ips_multi_async
(*addr: Union[str, ipaddress.IPv4Address, ipaddress.IPv6Address], version: Union[str, int] = 'any', v4_convert=False) → AsyncGenerator[Tuple[str, Optional[List[str]]], None][source]¶ Async version of
resolve_ips_multi()
. Resolve IPv4/v6 addresses for multiple hosts specified as positional arguments.Returns results as an AsyncIO generator, to allow for efficient handling of a large amount of hostnames to resolve.
Using the AsyncIO generator in a loop efficiently:
>>> async for host, ips in resolve_ips_multi_async('privex.io', 'cloudflare.com', 'google.com'): ... print(f"{host:<20} -> {', '.join(ips)}") ... privex.io -> 2a07:e00::abc, 185.130.44.10 cloudflare.com -> 2606:4700::6811:af55, 2606:4700::6811:b055, 104.17.176.85, 104.17.175.85 google.com -> 2a00:1450:4009:807::200e, 216.58.204.238
If you’re only resolving a small number of hosts ( less than 100 or so ), you can simply comprehend the generator into a
list
and then turn the result into adict
usingdict()
, which will get you a dictionary of hosts mapped to lists of IP addresses.Dictionary Cast Examples:
>>> ips = [x async for x in resolve_ips_multi_async('privex.io', 'microsoft.com', 'google.com')] >>> dict(ips) {'privex.io': ['2a07:e00::abc', '185.130.44.10'], 'microsoft.com': ['104.215.148.63', '40.76.4.15', '40.112.72.205', '40.113.200.201', '13.77.161.179'], 'google.com': ['2a00:1450:4009:807::200e', '216.58.204.238']} >>> dict([x async for x in resolve_ips_multi_async('privex.io', 'microsoft.com', 'google.com', version='v6')]) {'privex.io': ['2a07:e00::abc'], 'microsoft.com': [], 'google.com': ['2a00:1450:4009:81c::200e']} >>> dict([x async for x in resolve_ips_multi_async('privex.io', 'this-does-not-exist', 'google.com', version='v6')]) {'privex.io': ['2a07:e00::abc'], 'this-does-not-exist': [], 'google.com': ['2a00:1450:4009:81c::200e']} >>> dict([x async for x in resolve_ips_multi_async('privex.io', 'example.com', '127.0.0.1', version='v6')]) [resolve_ips_multi AttributeError] Invalid IP: 127.0.0.1 - Ex: <class 'AttributeError'> Passed address '127.0.0.1' was an IPv4 address, but 'version' requested an IPv6 address. {'privex.io': ['2a07:e00::abc'], 'example.com': ['2606:2800:220:1:248:1893:25c8:1946'], '127.0.0.1': None}
- Parameters
addr (str|IPv4Address|IPv6Address) – Hostname’s to resolve / IP addresses to validate or pass-thru
version (str|int) – (Default:
any
) -4
(int),'v4'
,6
(int),'v6'
(seeresolve_ips()
for more options)v4_convert (bool) – (Default:
False
) If set toTrue
, will allow IPv6-wrapped IPv4 addresses starting with::ffff:
to be returned when requesting versionv6
from an IPv4-only hostname.
- Return Tuple[str,Optional[List[str]] gen
An async generator which returns tuples containing a hostname/IP, and a list of it’s resolved IPs. If the IP was rejected (e.g. IPv4 IP passed with
v6
version
param), then the list may instead beNone
.