feat: add support for sending to a specific addr/port with ServiceInf… · python-zeroconf/python-zeroconf@8f5efad
@@ -55,6 +55,7 @@
5555_DNS_OTHER_TTL,
5656_FLAGS_QR_QUERY,
5757_LISTENER_TIME,
58+_MDNS_PORT,
5859_TYPE_A,
5960_TYPE_AAAA,
6061_TYPE_NSEC,
@@ -616,7 +617,12 @@ def _is_complete(self) -> bool:
616617return bool(self.text is not None and (self._ipv4_addresses or self._ipv6_addresses))
617618618619def request(
619-self, zc: 'Zeroconf', timeout: float, question_type: Optional[DNSQuestionType] = None
620+self,
621+zc: 'Zeroconf',
622+timeout: float,
623+question_type: Optional[DNSQuestionType] = None,
624+addr: Optional[str] = None,
625+port: int = _MDNS_PORT,
620626 ) -> bool:
621627"""Returns true if the service could be discovered on the
622628 network, and updates this object with details discovered.
@@ -628,13 +634,29 @@ def request(
628634assert zc.loop is not None and zc.loop.is_running()
629635if zc.loop == get_running_loop():
630636raise RuntimeError("Use AsyncServiceInfo.async_request from the event loop")
631-return bool(run_coro_with_timeout(self.async_request(zc, timeout, question_type), zc.loop, timeout))
637+return bool(
638+run_coro_with_timeout(
639+self.async_request(zc, timeout, question_type, addr, port), zc.loop, timeout
640+ )
641+ )
632642633643async def async_request(
634-self, zc: 'Zeroconf', timeout: float, question_type: Optional[DNSQuestionType] = None
644+self,
645+zc: 'Zeroconf',
646+timeout: float,
647+question_type: Optional[DNSQuestionType] = None,
648+addr: Optional[str] = None,
649+port: int = _MDNS_PORT,
635650 ) -> bool:
636651"""Returns true if the service could be discovered on the
637652 network, and updates this object with details discovered.
653+654+ This method will be run in the event loop.
655+656+ Passing addr and port is optional, and will default to the
657+ mDNS multicast address and port. This is useful for directing
658+ requests to a specific host that may be able to respond across
659+ subnets.
638660 """
639661if not zc.started:
640662await zc.async_wait_for_start()
@@ -658,7 +680,7 @@ async def async_request(
658680first_request = False
659681if not out.questions:
660682return self.load_from_cache(zc)
661-zc.async_send(out)
683+zc.async_send(out, addr, port)
662684next_ = now + delay
663685delay *= 2
664686next_ += random.randint(*_AVOID_SYNC_DELAY_RANDOM_INTERVAL)