resolve_ips_multi¶
-
privex.helpers.net.
resolve_ips_multi
(*addr: Union[str, ipaddress.IPv4Address, ipaddress.IPv6Address], version: Union[str, int] = 'any', v4_convert=False) → Generator[Tuple[str, Optional[List[str]]], None, None][source]¶ Resolve IPv4/v6 addresses for multiple hosts specified as positional arguments.
Returns results as a generator, to allow for efficient handling of a large amount of hostnames to resolve.
Using the generator in a loop efficiently:
>>> for host, ips in resolve_ips_multi('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 cast the generator into a
dict
usingdict()
, which will get you a dictionary of hosts mapped to lists of IP addresses.Dictionary Cast Examples:
>>> dict(resolve_ips_multi('privex.io', 'microsoft.com', 'google.com')) {'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(resolve_ips_multi('privex.io', 'microsoft.com', 'google.com', version='v6')) {'privex.io': ['2a07:e00::abc'], 'microsoft.com': [], 'google.com': ['2a00:1450:4009:81c::200e']} >>> dict(resolve_ips_multi('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(resolve_ips_multi('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 to resolve / IP address 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
A 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
.