Created
December 5, 2011 22:07
-
-
Save provegard/1435601 to your computer and use it in GitHub Desktop.
Test program for using Avahi in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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