Skip to content

Instantly share code, notes, and snippets.

@dpogue
Created January 30, 2011 10:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpogue/802742 to your computer and use it in GitHub Desktop.
Save dpogue/802742 to your computer and use it in GitHub Desktop.
Python script to watch for Avahi/Bonjour workstation broadcasts and present alerts using libnotify
#!/usr/bin/env python
import dbus, gobject, avahi, pynotify, time
from dbus import DBusException
from dbus.mainloop.glib import DBusGMainLoop
TYPE = '_workstation._tcp'
def service_resolved(*args):
name = args[5].split('.')[0]
ip = args[7]
n = pynotify.Notification("'%s' has joined the network" % name, ip, 'computer')
n.set_urgency(pynotify.URGENCY_LOW)
n.set_timeout(3)
n.show()
def print_error(*args):
print 'error_handler'
print args[0]
def myhandler(interface, protocol, name, stype, domain, flags):
if flags & avahi.LOOKUP_RESULT_LOCAL:
# local service, skip
pass
server.ResolveService(interface, protocol, name, stype,
domain, avahi.PROTO_INET, dbus.UInt32(0),
reply_handler=service_resolved, error_handler=print_error)
loop = DBusGMainLoop()
bus = dbus.SystemBus(mainloop=loop)
server = dbus.Interface( bus.get_object(avahi.DBUS_NAME, '/'),
'org.freedesktop.Avahi.Server')
sbrowser = dbus.Interface(bus.get_object(avahi.DBUS_NAME,
server.ServiceBrowserNew(avahi.IF_UNSPEC,
avahi.PROTO_INET, TYPE, 'local', dbus.UInt32(0))),
avahi.DBUS_INTERFACE_SERVICE_BROWSER)
sbrowser.connect_to_signal("ItemNew", myhandler)
gobject.MainLoop().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment