mini.dns.zeroconf module

Multicast DNS Service Discovery for Python, v0.14-wmcbrine Copyright 2003 Paul Scott-Murphy, 2014 William McBrine

This module provides a framework for the use of DNS Service Discovery using IP multicast.

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

class mini.dns.zeroconf.Zeroconf(interfaces: List[str | int] | InterfaceChoice = InterfaceChoice.All, unicast: bool = False, ip_version: IPVersion | None = None, apple_p2p: bool = False)[源代码]

基类:QuietLogger

Implementation of Zeroconf Multicast DNS Service Discovery

Supports registration, unregistration, queries and browsing.

property done: bool
wait(timeout: float) None[源代码]

Calling thread waits for a given number of milliseconds or until notified.

notify_all() None[源代码]

Notifies all waiting threads

get_service_info(type_: str, name: str, timeout: int = 3000) ServiceInfo | None[源代码]

Returns network’s service information for a particular name and type, or None if no service matches by the timeout, which defaults to 3 seconds.

add_service_listener(type_: str, listener: ServiceListener) None[源代码]

Adds a listener for a particular service type. This object will then have its add_service and remove_service methods called when services of that type become available and unavailable.

remove_service_listener(listener: ServiceListener) None[源代码]

Removes a listener from the set that is currently listening.

remove_all_service_listeners() None[源代码]

Removes a listener from the set that is currently listening.

register_service(info: ServiceInfo, ttl: int | None = None, allow_name_change: bool = False, cooperating_responders: bool = False) None[源代码]

Registers service information to the network with a default TTL. Zeroconf will then respond to requests for information for that service. The name of the service may be changed if needed to make it unique on the network. Additionally multiple cooperating responders can register the same service on the network for resilience (if you want this behavior set cooperating_responders to True).

update_service(info: ServiceInfo) None[源代码]

Registers service information to the network with a default TTL. Zeroconf will then respond to requests for information for that service.

unregister_service(info: ServiceInfo) None[源代码]

Unregister a service.

unregister_all_services() None[源代码]

Unregister all registered services.

check_service(info: ServiceInfo, allow_name_change: bool, cooperating_responders: bool = False) None[源代码]

Checks the network for a unique service name, modifying the ServiceInfo passed in if it is not unique.

add_listener(listener: RecordUpdateListener, question: DNSQuestion | None) None[源代码]

Adds a listener for a given question. The listener will have its update_record method called when information is available to answer the question.

remove_listener(listener: RecordUpdateListener) None[源代码]

Removes a listener.

update_record(now: float, rec: DNSRecord) None[源代码]

Used to notify listeners of new information that has updated a record.

handle_response(msg: DNSIncoming) None[源代码]

Deal with incoming response packets. All answers are held in the cache, and listeners are notified.

handle_query(msg: DNSIncoming, addr: str | None, port: int) None[源代码]

Deal with incoming query packets. Provides a response if possible.

send(out: DNSOutgoing, addr: str | None = None, port: int = 5353) None[源代码]

Sends an outgoing packet.

close() None[源代码]

Ends the background threads, and prevent this instance from servicing further queries.

class mini.dns.zeroconf.ServiceInfo(type_: str, name: str, address: bytes | List[bytes] | None = None, port: int | None = None, weight: int = 0, priority: int = 0, properties: bytes | Dict = b'', server: str | None = None, host_ttl: int = 120, other_ttl: int = 4500, *, addresses: List[bytes] | None = None)[源代码]

基类:RecordUpdateListener

Service information.

Constructor parameters are as follows:

  • type_: fully qualified service type name

  • name: fully qualified service name

  • address: IP address as unsigned short, network byte order (deprecated, use addresses)

  • port: port that the service runs on

  • weight: weight of the service

  • priority: priority of the service

  • properties: dictionary of properties (or a bytes object holding the contents of the text field). converted to str and then encoded to bytes using UTF-8. Keys with None values are converted to value-less attributes.

  • server: fully qualified name for service host (defaults to name)

  • host_ttl: ttl used for A/SRV records

  • other_ttl: ttl used for PTR/TXT records

  • addresses: List of IP addresses as unsigned short (IPv4) or unsigned 128 bit number (IPv6), network byte order

text = b''
property address: bytes | None
property addresses: List[bytes]

IPv4 addresses of this service.

Only IPv4 addresses are returned for backward compatibility. Use addresses_by_version() or parsed_addresses() to include IPv6 addresses as well.

property properties: Dict

If properties were set in the constructor this property returns the original dictionary of type Dict[Union[bytes, str], Any].

If properties are coming from the network, after decoding a TXT record, the keys are always bytes and the values are either bytes, if there was a value, even empty, or None, if there was none. No further decoding is attempted. The type returned is Dict[bytes, Optional[bytes]].

addresses_by_version(version: IPVersion) List[bytes][源代码]

List addresses matching IP version.

parsed_addresses(version: IPVersion = IPVersion.All) List[str][源代码]

List addresses in their parsed string form.

get_name() str[源代码]

Name accessor

update_record(zc: Zeroconf, now: float, record: DNSRecord | None) None[源代码]

Updates service information from a DNS record

request(zc: Zeroconf, timeout: float) bool[源代码]

Returns true if the service could be discovered on the network, and updates this object with details discovered.

class mini.dns.zeroconf.ServiceBrowser(zc: Zeroconf, type_: str, handlers: ServiceListener | List[Callable[[...], None]] | None = None, listener: ServiceListener | None = None, addr: str | None = None, port: int = 5353, delay: int = 1000)[源代码]

基类:RecordUpdateListener, Thread

Used to browse for a service of a specific type.

The listener object will have its add_service() and remove_service() methods called when this browser discovers changes in the services availability.

property service_state_changed: SignalRegistrationInterface
update_record(zc: Zeroconf, now: float, record: DNSRecord) None[源代码]

Callback invoked by Zeroconf when new information arrives.

Updates information required by browser in the Zeroconf cache.

Ensures that there is are no unecessary duplicates in the list

cancel() None[源代码]
run() None[源代码]

Method representing the thread’s activity.

You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.

exception mini.dns.zeroconf.Error[源代码]

基类:Exception

class mini.dns.zeroconf.InterfaceChoice(value)[源代码]

基类:Enum

An enumeration.

Default = 1
All = 2
class mini.dns.zeroconf.ServiceStateChange(value)[源代码]

基类:Enum

An enumeration.

Added = 1
Removed = 2
Updated = 3
class mini.dns.zeroconf.IPVersion(value)[源代码]

基类:Enum

An enumeration.

V4Only = 1
V6Only = 2
All = 3