add real-time search

This commit is contained in:
vvzvlad 2023-05-28 14:17:43 +07:00
parent 00fdfb6298
commit 1dc2a8594a

View File

@ -15,6 +15,9 @@ class MyListener:
def add_service(self, zeroconf, type, name): def add_service(self, zeroconf, type, name):
info = zeroconf.get_service_info(type, name) info = zeroconf.get_service_info(type, name)
self.found_services.append(info) self.found_services.append(info)
print(f"Name: {info.name}")
print(f"IP: {info.parsed_addresses()[0]}")
print("")
def update_service(self, zeroconf, type, name): def update_service(self, zeroconf, type, name):
pass pass
@ -24,25 +27,14 @@ def main():
listener = MyListener() listener = MyListener()
browser = ServiceBrowser(zeroconf, "_workstation._tcp.local.", listener) browser = ServiceBrowser(zeroconf, "_workstation._tcp.local.", listener)
# Устанавливаем время ожидания и ждем, пока не найдутся все службы try:
timeout = 15 while True:
wait_event = Event() time.sleep(0.1)
wait_event.wait(timeout) except KeyboardInterrupt:
pass
# Закрываем браузер служб и освобождаем ресурсы finally:
browser.cancel() browser.cancel()
zeroconf.close() zeroconf.close()
# Выводим список найденных служб
print("Found services:")
for service in listener.found_services:
print(f"Name: {service.name}")
print(f"IP: {service.parsed_addresses()[0]}")
print("")
if __name__ == "__main__": if __name__ == "__main__":
main() main()