Skip to content

Instantly share code, notes, and snippets.

@provegard
Created December 5, 2011 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save provegard/1435601 to your computer and use it in GitHub Desktop.
Save provegard/1435601 to your computer and use it in GitHub Desktop.
Test program for using Avahi in Python
#!/usr/bin/python
# Test program for using Avahi in Python. Publishes a dummy service.
import sys
import avahi
import dbus
from twisted.internet import reactor
# Service details
name = "Avahi Interface Test"
port = 12345
stype = "_avahitest._tcp"
domain = ""
host = ""
text = ["hello=world"]
class AvahiInterfaceTest(object):
def __init__(self, iface):
print "Using network interface", iface
self.iface = iface
def start(self):
bus = dbus.SystemBus()
server = dbus.Interface(bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
g = dbus.Interface(bus.get_object(avahi.DBUS_NAME, server.EntryGroupNew()), avahi.DBUS_INTERFACE_ENTRY_GROUP)
g.AddService(self.iface, avahi.PROTO_UNSPEC, dbus.UInt32(0), name, stype, domain, host, dbus.UInt16(port), avahi.string_array_to_txt_array(text))
g.Commit()
self.group = g
print "Avahi service has been published. Press Ctrl-C to quit."
def stop(self):
print "Stopping..."
self.group.Reset()
if __name__ == "__main__":
iface = avahi.IF_UNSPEC
if len(sys.argv) >= 2:
iface = int(sys.argv[1])
ait = AvahiInterfaceTest(iface)
reactor.callWhenRunning(ait.start)
reactor.addSystemEventTrigger('before', 'shutdown', ait.stop)
reactor.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment