ZeroConf is a pure Python implementation of multicast DNS service discovery (mDNS/DNS-SD), commonly known as Bonjour or Apple's Zero Configuration Networking. It allows applications to discover services on a local network without manual configuration. The library is currently at version 0.148.0 and maintains an active release cadence with frequent bug fixes and feature updates.
pip install zeroconfVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to register a local HTTP service and simultaneously browse for all HTTP services on the network. It registers 'MyTestService' on port 8080 and then listens for 30 seconds, printing any discovered or removed services. Remember to handle `KeyboardInterrupt` and ensure `Zeroconf` instances are properly closed to release network resources.
Ensure your `pip install` command does not explicitly target `zeroconf==1.0.0`. Stick to `zeroconf` or `zeroconf~=0.x.x`.
Always wrap your `Zeroconf` usage in a `try...finally` block to ensure `zeroconf.close()` is called, as shown in the quickstart example. Consider using `asyncio` for long-running applications or running `Zeroconf` in a dedicated thread.
Pass a list of interface IPs (e.g., `interfaces=['192.168.1.2', '10.0.0.5']`) to the `Zeroconf` constructor: `zeroconf = Zeroconf(interfaces=my_interfaces)`.
pip install zeroconf
Ensure `zeroconf_instance.close()` is called when the Zeroconf instance is no longer needed, typically in a `try...finally` block, or by using it as a context manager (e.g., `with Zeroconf(...) as zc:`).
class MyListener:
def add_service(self, zeroconf_instance, service_type, service_name):
info = zeroconf_instance.get_service_info(service_type, service_name)
if info:
print(f'Service found: {info.name}')
# Correct usage:
# zeroconf_instance = zeroconf.Zeroconf()
# browser = zeroconf.ServiceBrowser(zeroconf_instance, "_http._tcp.local.", MyListener())
# ...
# zeroconf_instance.close()# Check available interfaces (e.g., using 'ip a' on Linux, 'ipconfig' on Windows, 'ifconfig' on macOS).
# Use a valid interface name, or omit the 'interfaces' argument to bind to all available interfaces.
import zeroconf
# Example with a valid interface (replace 'en0' with your actual interface):
# from ipaddress import ip_address
# zc = zeroconf.Zeroconf(interfaces=[ip_address("192.168.1.10")])
# Or, to bind to all interfaces (most common):
zc = zeroconf.Zeroconf()