5 # This file is part of avahi.
7 # avahi is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU Lesser General Public License as
9 # published by the Free Software Foundation; either version 2 of the
10 # License, or (at your option) any later version.
12 # avahi is distributed in the hope that it will be useful, but WITHOUT
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 # License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with avahi; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 import avahi, gobject, dbus
27 print "Sorry, to use this tool you need to install Avahi, pygtk and python-dbus."
32 except ImportError, e:
35 service_type_browsers = {}
38 def siocgifname(interface):
44 return server.GetNetworkInterfaceNameByIndex(interface)
46 def service_resolved(interface, protocol, name, type, domain, host, aprotocol, address, port, txt):
47 print "Service data for service '%s' of type '%s' in domain '%s' on %s.%i:" % (name, type, domain, siocgifname(interface), protocol)
48 print "\tHost %s (%s), port %i, TXT data: %s" % (host, address, port, str(txt))
51 print "Error:", str(err)
53 def new_service(interface, protocol, name, type, domain):
56 print "Found service '%s' of type '%s' in domain '%s' on %s.%i." % (name, type, domain, siocgifname(interface), protocol)
58 # Asynchronous resolving
59 server.ResolveService(interface, protocol, name, type, domain, avahi.PROTO_UNSPEC, reply_handler=service_resolved, error_handler=print_error)
61 def remove_service(interface, protocol, name, type, domain):
62 print "Service '%s' of type '%s' in domain '%s' on %s.%i disappeared." % (name, type, domain, siocgifname(interface), protocol)
64 def new_service_type(interface, protocol, type, domain):
65 global server, service_browsers
67 # Are we already browsing this domain for this type?
68 if service_browsers.has_key((interface, protocol, type, domain)):
71 print "Browsing for services of type '%s' in domain '%s' on %s.%i ..." % (type, domain, siocgifname(interface), protocol)
73 b = dbus.Interface(bus.get_object(avahi.DBUS_NAME, server.ServiceBrowserNew(interface, protocol, type, domain)), avahi.DBUS_INTERFACE_SERVICE_BROWSER)
74 b.connect_to_signal('ItemNew', new_service)
75 b.connect_to_signal('ItemRemove', remove_service)
77 service_browsers[(interface, protocol, type, domain)] = b
79 def browse_domain(interface, protocol, domain):
80 global server, service_type_browsers
82 # Are we already browsing this domain?
83 if service_type_browsers.has_key((interface, protocol, domain)):
86 print "Browsing domain '%s' on %s.%i ..." % (domain, siocgifname(interface), protocol)
88 b = dbus.Interface(bus.get_object(avahi.DBUS_NAME, server.ServiceTypeBrowserNew(interface, protocol, domain)), avahi.DBUS_INTERFACE_SERVICE_TYPE_BROWSER)
89 b.connect_to_signal('ItemNew', new_service_type)
91 service_type_browsers[(interface, protocol, domain)] = b
93 def new_domain(interface, protocol, domain):
95 # We browse for .local anyway...
97 browse_domain(interface, protocol, domain)
102 if len(sys.argv) > 1:
105 bus = dbus.SystemBus()
106 server = dbus.Interface(bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
109 # Explicitly browse .local
110 browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "local")
112 # Browse for other browsable domains
113 db = dbus.Interface(bus.get_object(avahi.DBUS_NAME, server.DomainBrowserNew(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "", avahi.DOMAIN_BROWSER_BROWSE)), avahi.DBUS_INTERFACE_DOMAIN_BROWSER)
114 db.connect_to_signal('ItemNew', new_domain)
117 # Just browse the domain the user wants us to browse
118 browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, domain)
121 gobject.MainLoop().run()
122 except KeyboardInterrupt, k: