#! /usr/bin/python -tt # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Library General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # # # Copyright Red Hat Inc. 2008 # # Author: James Antill # Based on http://avahi.org/wiki/PythonBrowseExample import dbus, gobject, avahi from dbus import DBusException from dbus.mainloop.glib import DBusGMainLoop __DNS__ = "_checksum_data._tcp" def found(interface, protocol, name, stype, domain, flags): """ Found some data from Avahi. """ print "=" * 79 if flags & avahi.LOOKUP_RESULT_LOCAL: print "%s%s" % (" " * 37, "LOCAL") # print "JDBG:", interface, protocol, name, stype, domain, flags # Grab the information about the service info = server.ResolveService(interface, protocol, name, stype, domain, avahi.PROTO_UNSPEC, dbus.UInt32(0)) ip = info[7] port = info[8] name = info[2] domain = info[3] # print "JDBG:", info url = "http://%s:%s/" % (str(ip), str(port)) print url print " %s: %s" % (name, domain) print "-" * 79 def all_done(): # Avahi callbacks finished mainloop.quit() loop = DBusGMainLoop() bus = dbus.SystemBus(mainloop=loop) server = dbus.Interface(bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER) path = server.ServiceBrowserNew(avahi.IF_UNSPEC, avahi.PROTO_INET, __DNS__, "local", dbus.UInt32(0)) sbrowser = dbus.Interface(bus.get_object(avahi.DBUS_NAME, path), avahi.DBUS_INTERFACE_SERVICE_BROWSER) sbrowser.connect_to_signal('ItemNew', found) sbrowser.connect_to_signal('AllForNow', all_done) mainloop = gobject.MainLoop() try: mainloop.run() except KeyboardInterrupt: pass sbrowser.Free()