From 84b591be3818ec6957f0528cb5c944a3f776bdf4 Mon Sep 17 00:00:00 2001 From: vvzvlad Date: Mon, 17 Apr 2023 15:22:20 +0700 Subject: [PATCH] add test mdns search --- mdns_search.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 3 +++ 2 files changed, 51 insertions(+) create mode 100755 mdns_search.py diff --git a/mdns_search.py b/mdns_search.py new file mode 100755 index 0000000..6b7a359 --- /dev/null +++ b/mdns_search.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python3 +import sys +import time +from zeroconf import ServiceBrowser, Zeroconf +from threading import Event + +class MyListener: + + def __init__(self): + self.found_services = [] + + def remove_service(self, zeroconf, type, name): + pass + + def add_service(self, zeroconf, type, name): + info = zeroconf.get_service_info(type, name) + self.found_services.append(info) + + def update_service(self, zeroconf, type, name): + pass + +def main(): + zeroconf = Zeroconf() + listener = MyListener() + browser = ServiceBrowser(zeroconf, "_workstation._tcp.local.", listener) + + # Устанавливаем время ожидания и ждем, пока не найдутся все службы + timeout = 5 + 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("") + +if __name__ == "__main__": + main() + + + + diff --git a/requirements.txt b/requirements.txt index cb3d1b3..5ac4a50 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +async-timeout==4.0.2 bcrypt==4.0.1 certifi==2022.12.7 cffi==1.15.1 @@ -5,9 +6,11 @@ charset-normalizer==3.1.0 cryptography==40.0.2 fabric==3.0.0 idna==3.4 +ifaddr==0.2.0 invoke==2.0.0 paramiko==3.1.0 pycparser==2.21 PyNaCl==1.5.0 requests==2.28.2 urllib3==1.26.15 +zeroconf==0.56.0