DeaD_EyE

zeroconf

May 6th, 2020
69
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from ipaddress import ip_address
  2.  
  3. from zeroconf import Zeroconf, IPVersion
  4. from netifaces import ifaddresses, interfaces, AF_INET
  5.  
  6. service_xml_file = """
  7. <?xml version="1.0" standalone='no'?><!--*-nxml-*-->
  8. <!DOCTYPE service-group SYSTEM "avahi-service.dtd">
  9. <service-group>
  10. <name replace-wildcards="yes">TEST</name>
  11.   <service>
  12.       <type>_http._tcp</type>
  13.           <port>80</port>
  14.             </service>
  15.             </service-group>
  16. """
  17.  
  18.  
  19. def get_services():
  20.     service_name = "TEST"
  21.     service_type = "_http._tcp.local."
  22.     zeroconf = Zeroconf(ip_version=IPVersion.V4Only)
  23.     result = zeroconf.get_service_info(service_type, f"{service_name}.{service_type}", 1000)
  24.     if result:
  25.         port = result.port
  26.         ips = [ip_address(ip) for ip in result.parsed_addresses(IPVersion.V4Only)]
  27.         return set(ips) - get_local_ips()
  28.     else:
  29.         return set()
  30.  
  31.  
  32. def get_local_ips():
  33.     ips = set()
  34.     for interface in interfaces():
  35.         result = ifaddresses(interface)
  36.         af_inet = result.get(AF_INET)
  37.         if af_inet:
  38.             for addr in af_inet:
  39.                 address = addr.get("addr")
  40.                 ips.add(ip_address(address))
  41.     return ips
  42.  
  43.  
  44.  
  45.  
  46. print(get_services())
RAW Paste Data