resolve_ips¶
-
privex.helpers.net.
resolve_ips
(addr: Union[str, ipaddress.IPv4Address, ipaddress.IPv6Address], version: Union[str, int] = 'any', v4_convert=False) → List[str][source]¶ With just a single hostname argument, both IPv4 and IPv6 addresses will be returned as strings:
>>> resolve_ips('www.privex.io') ['2a07:e00::abc', '185.130.44.10']
You can provide the
version
argument as either positional or kwarg, e.g.v4
orv6
to restrict the results to only that IP version:>>> resolve_ips('privex.io', version='v4') ['185.130.44.10']
The
v4_convert
option isFalse
by default, which prevents::ffff:
style IPv6 wrapped IPv4 addresses being returned when you request versionv6
:>>> resolve_ips('microsoft.com') ['40.76.4.15', '40.112.72.205', '13.77.161.179', '40.113.200.201', '104.215.148.63'] >>> resolve_ips('microsoft.com', 'v6') []
If for whatever reason, you need
::ffff:
IPv6 wrapped IPv4 addresses to be returned, you can setv4_convert=True
, which will disable filtering out::ffff:
fake IPv6 addresses:>>> resolve_ips('microsoft.com', 'v6', v4_convert=True) ['::ffff:40.76.4.15', '::ffff:40.112.72.205', '::ffff:13.77.161.179', '::ffff:40.113.200.201', '::ffff:104.215.148.63']
For convenience, if an IPv4 / IPv6 address is specified, then it will simply be validated against
version
and then returned within a list. This is useful when handling user specified data, which may be either a hostname or an IP address, and you need to resolve hostnames while leaving IP addresses alone:>>> resolve_ips('8.8.4.4') ['8.8.4.4'] >>> resolve_ips('2a07:e00::333') ['2a07:e00::333'] >>> resolve_ips('8.8.4.4', 'v6') Traceback (most recent call last): File "<ipython-input-10-6ca9e766006f>", line 1, in <module> resolve_ips('8.8.4.4', 'v6') AttributeError: Passed address '8.8.4.4' was an IPv4 address, but 'version' requested an IPv6 address. >>> resolve_ips('2a07:e00::333', 'v4') Traceback (most recent call last): File "<ipython-input-11-543bfa71c57a>", line 1, in <module> resolve_ips('2a07:e00::333', 'v4') AttributeError: Passed address '2a07:e00::333' was an IPv6 address, but 'version' requested an IPv4 address.
- Parameters
addr (str|IPv4Address|IPv6Address) – The hostname to resolve. If an IPv4 / IPv6 address is passed instead of a hostname, it will be validated against
version
, then returned in a single item list.version (str|int) –
Default:
'any'
- Return both IPv4 and IPv6 addresses (if both are found). If an IP address is passed, then both IPv4 and IPv6 addresses will be accepted. If set to one of the IPv4/IPv6 version choices, then a passed IP of the wrong version will raiseAttributeError
Choices:
IPv4 Options:
4
(int),'v4'
,'4'
(str),'ipv4'
,'inet'
,'inet4'
IPv6 Options:
6
(int),'v6'
,'6'
(str),'ipv6'
,'inet6'
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.
- Raises
AttributeError – Raised when an IPv4 address is passed and
version
is set to IPv6 - as well as vice versa (IPv6 passed while version is set to IPv4)- Return List[str] ips
Zero or more IP addresses in a list of
str
’s