From 1dc2a8594afe4c7432d12800cefdf1ce2a226b53 Mon Sep 17 00:00:00 2001 From: vvzvlad Date: Sun, 28 May 2023 14:17:43 +0700 Subject: [PATCH] add real-time search --- mdns_search.py | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/mdns_search.py b/mdns_search.py index 5e4a3e2..f225e83 100755 --- a/mdns_search.py +++ b/mdns_search.py @@ -15,6 +15,9 @@ class MyListener: def add_service(self, zeroconf, type, name): info = zeroconf.get_service_info(type, name) 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): pass @@ -24,25 +27,14 @@ def main(): listener = MyListener() browser = ServiceBrowser(zeroconf, "_workstation._tcp.local.", listener) - # Устанавливаем время ожидания и ждем, пока не найдутся все службы - timeout = 15 - wait_event = Event() - wait_event.wait(timeout) - - # Закрываем браузер служб и освобождаем ресурсы - browser.cancel() - zeroconf.close() - - # Выводим список найденных служб - print("Found services:") - for service in listener.found_services: - print(f"Name: {service.name}") - print(f"IP: {service.parsed_addresses()[0]}") - print("") + try: + while True: + time.sleep(0.1) + except KeyboardInterrupt: + pass + finally: + browser.cancel() + zeroconf.close() if __name__ == "__main__": main() - - - -