feat: add simple address resolvers and examples (#1499) · python-zeroconf/python-zeroconf@ae3c352

@@ -88,6 +88,10 @@

8888

# the A/AAAA/SRV records for a host.

8989

_AVOID_SYNC_DELAY_RANDOM_INTERVAL = (20, 120)

909091+

_TYPE_AAAA_RECORDS = {_TYPE_AAAA}

92+

_TYPE_A_RECORDS = {_TYPE_A}

93+

_TYPE_A_AAAA_RECORDS = {_TYPE_A, _TYPE_AAAA}

94+9195

bytes_ = bytes

9296

float_ = float

9397

int_ = int

@@ -146,6 +150,7 @@ class ServiceInfo(RecordUpdateListener):

146150

"_name",

147151

"_new_records_futures",

148152

"_properties",

153+

"_query_record_types",

149154

"host_ttl",

150155

"interface_index",

151156

"key",

@@ -210,6 +215,7 @@ def __init__(

210215

self._dns_service_cache: Optional[DNSService] = None

211216

self._dns_text_cache: Optional[DNSText] = None

212217

self._get_address_and_nsec_records_cache: Optional[Set[DNSRecord]] = None

218+

self._query_record_types = {_TYPE_SRV, _TYPE_TXT, _TYPE_A, _TYPE_AAAA}

213219214220

@property

215221

def name(self) -> str:

@@ -917,18 +923,22 @@ def _generate_request_query(

917923

cache = zc.cache

918924

history = zc.question_history

919925

qu_question = question_type is QU_QUESTION

920-

self._add_question_with_known_answers(

921-

out, qu_question, history, cache, now, name, _TYPE_SRV, _CLASS_IN, True

922-

)

923-

self._add_question_with_known_answers(

924-

out, qu_question, history, cache, now, name, _TYPE_TXT, _CLASS_IN, True

925-

)

926-

self._add_question_with_known_answers(

927-

out, qu_question, history, cache, now, server, _TYPE_A, _CLASS_IN, False

928-

)

929-

self._add_question_with_known_answers(

930-

out, qu_question, history, cache, now, server, _TYPE_AAAA, _CLASS_IN, False

931-

)

926+

if _TYPE_SRV in self._query_record_types:

927+

self._add_question_with_known_answers(

928+

out, qu_question, history, cache, now, name, _TYPE_SRV, _CLASS_IN, True

929+

)

930+

if _TYPE_TXT in self._query_record_types:

931+

self._add_question_with_known_answers(

932+

out, qu_question, history, cache, now, name, _TYPE_TXT, _CLASS_IN, True

933+

)

934+

if _TYPE_A in self._query_record_types:

935+

self._add_question_with_known_answers(

936+

out, qu_question, history, cache, now, server, _TYPE_A, _CLASS_IN, False

937+

)

938+

if _TYPE_AAAA in self._query_record_types:

939+

self._add_question_with_known_answers(

940+

out, qu_question, history, cache, now, server, _TYPE_AAAA, _CLASS_IN, False

941+

)

932942

return out

933943934944

def __repr__(self) -> str:

@@ -954,3 +964,45 @@ def __repr__(self) -> str:

954964955965

class AsyncServiceInfo(ServiceInfo):

956966

"""An async version of ServiceInfo."""

967+968+969+

class AddressResolver(ServiceInfo):

970+

"""Resolve a host name to an IP address."""

971+972+

def __init__(self, server: str) -> None:

973+

"""Initialize the AddressResolver."""

974+

super().__init__(server, server, server=server)

975+

self._query_record_types = _TYPE_A_AAAA_RECORDS

976+977+

@property

978+

def _is_complete(self) -> bool:

979+

"""The ServiceInfo has all expected properties."""

980+

return bool(self._ipv4_addresses) or bool(self._ipv6_addresses)

981+982+983+

class AddressResolverIPv6(ServiceInfo):

984+

"""Resolve a host name to an IPv6 address."""

985+986+

def __init__(self, server: str) -> None:

987+

"""Initialize the AddressResolver."""

988+

super().__init__(server, server, server=server)

989+

self._query_record_types = _TYPE_AAAA_RECORDS

990+991+

@property

992+

def _is_complete(self) -> bool:

993+

"""The ServiceInfo has all expected properties."""

994+

return bool(self._ipv6_addresses)

995+996+997+

class AddressResolverIPv4(ServiceInfo):

998+

"""Resolve a host name to an IPv4 address."""

999+1000+

def __init__(self, server: str) -> None:

1001+

"""Initialize the AddressResolver."""

1002+

super().__init__(server, server, server=server)

1003+

self._query_record_types = _TYPE_A_RECORDS

1004+1005+

@property

1006+

def _is_complete(self) -> bool:

1007+

"""The ServiceInfo has all expected properties."""

1008+

return bool(self._ipv4_addresses)