resolve_ip

privex.helpers.net.resolve_ip(addr: Union[str, ipaddress.IPv4Address, ipaddress.IPv6Address], version: Union[str, int] = 'any', v4_convert=False) → Optional[str][source]

Wrapper for resolve_ips() - passes args to resolve_ips() and returns the first item from the results.

If the results are empty, None will be returned.

Examples:

>>> resolve_ip('privex.io')
'2a07:e00::abc'
>>> resolve_ip('privex.io', 'v4')
'185.130.44.10'
>>> resolve_ip('microsoft.com')
'104.215.148.63'
>>> repr(resolve_ip('microsoft.com' ,'v6'))
'None'
>>> resolve_ip('microsoft.com' ,'v6', v4_convert=True)
'::ffff:104.215.148.63'
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' (see resolve_ips() for more options)

  • v4_convert (bool) – (Default: False) If set to True, will allow IPv6-wrapped IPv4 addresses starting with ::ffff: to be returned when requesting version v6 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 Optional[str] ips

An IPv4/v6 address as a string if there was at least 1 result - otherwise None.