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:

616617

return bool(self.text is not None and (self._ipv4_addresses or self._ipv6_addresses))

617618618619

def 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(

628634

assert zc.loop is not None and zc.loop.is_running()

629635

if zc.loop == get_running_loop():

630636

raise 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+

)

632642633643

async 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

"""

639661

if not zc.started:

640662

await zc.async_wait_for_start()

@@ -658,7 +680,7 @@ async def async_request(

658680

first_request = False

659681

if not out.questions:

660682

return self.load_from_cache(zc)

661-

zc.async_send(out)

683+

zc.async_send(out, addr, port)

662684

next_ = now + delay

663685

delay *= 2

664686

next_ += random.randint(*_AVOID_SYNC_DELAY_RANDOM_INTERVAL)