Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from ipaddress import ip_address
- from zeroconf import Zeroconf, IPVersion
- from netifaces import ifaddresses, interfaces, AF_INET
- service_xml_file = """
- <?xml version="1.0" standalone='no'?><!--*-nxml-*-->
- <!DOCTYPE service-group SYSTEM "avahi-service.dtd">
- <service-group>
- <name replace-wildcards="yes">TEST</name>
- <service>
- <type>_http._tcp</type>
- <port>80</port>
- </service>
- </service-group>
- """
- def get_services():
- service_name = "TEST"
- service_type = "_http._tcp.local."
- zeroconf = Zeroconf(ip_version=IPVersion.V4Only)
- result = zeroconf.get_service_info(service_type, f"{service_name}.{service_type}", 1000)
- if result:
- port = result.port
- ips = [ip_address(ip) for ip in result.parsed_addresses(IPVersion.V4Only)]
- return set(ips) - get_local_ips()
- else:
- return set()
- def get_local_ips():
- ips = set()
- for interface in interfaces():
- result = ifaddresses(interface)
- af_inet = result.get(AF_INET)
- if af_inet:
- for addr in af_inet:
- address = addr.get("addr")
- ips.add(ip_address(address))
- return ips
- print(get_services())
Add Comment
Please, Sign In to add comment