Skip to content

Instantly share code, notes, and snippets.

@silashansen
Last active August 11, 2023 12:03
Show Gist options
  • Save silashansen/ce82d539394463031941895c00044d39 to your computer and use it in GitHub Desktop.
Save silashansen/ce82d539394463031941895c00044d39 to your computer and use it in GitHub Desktop.
mDNS local network discovery

Instructions

Run:

python3 -m venv .venv

venv/bin/activate 

pip3 install -r requirements.txt

python3 app.py
from zeroconf import ServiceBrowser, ServiceListener, Zeroconf, ZeroconfServiceTypes
class MyListener(ServiceListener):
def update_service(self, zc: Zeroconf, type_: str, name: str) -> None:
info = zc.get_service_info(type_, name)
print("Updated: \t", format_info(info))
def remove_service(self, zc: Zeroconf, type_: str, name: str) -> None:
info = zc.get_service_info(type_, name)
print("Removed: \t", format_info(info))
def add_service(self, zc: Zeroconf, type_: str, name: str) -> None:
info = zc.get_service_info(type_, name)
print("Added: \t\t", format_info(info))
def format_info(info):
if info is None:
return "None"
str = "Server: \t" + info.server + ",\t\tIP: \t" + info.parsed_addresses()[0]
return str
zeroconf = Zeroconf()
listener = MyListener()
services = ZeroconfServiceTypes.find()
#map services into string array
services = list(services)
browser = ServiceBrowser(zeroconf, services, listener)
try:
input("Press enter to exit...\n\n")
finally:
zeroconf.close()
zeroconf==0.74.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment