Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a program to be broadcasted by avahi

Tags:

python

dbus

avahi

I'm trying to write a program that outputs data that can be served over a network with avahi. The documentation I've looked at seems to say I have to register the service with dbus and then connect it to avahi, but the documentation to do this is pretty sparse. Does anyone know of good documentation for it? I've been looking at these:

python-dbus: http://dbus.freedesktop.org/doc/dbus-python/doc/tutorial.html#exporting-objects

python-avahi: http://www.amk.ca/diary/2007/04/rough_notes_python_and_dbus.html

I'm really unfamiliar with how avahi works at all, so any pointers would be helpful.

like image 385
victor Avatar asked Oct 07 '09 22:10

victor


Video Answer


2 Answers

I realise this answer is pretty late, considering your question was asked four years ago. However, it might help others.

The following announces a service using avahi/dbus:

import avahi
import dbus
from time import sleep


class ServiceAnnouncer:
    def __init__(self, name, service, port, txt):
        bus = dbus.SystemBus()
        server = dbus.Interface(bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
        group = dbus.Interface(bus.get_object(avahi.DBUS_NAME, server.EntryGroupNew()),
                               avahi.DBUS_INTERFACE_ENTRY_GROUP)

        self._service_name = name
        index = 1
        while True:
            try:
                group.AddService(avahi.IF_UNSPEC, avahi.PROTO_INET, 0, self._service_name, service, '', '', port, avahi.string_array_to_txt_array(txt))
            except dbus.DBusException: # name collision -> rename
                index += 1
                self._service_name = '%s #%s' % (name, str(index))
            else:
                break

        group.Commit()

    def get_service_name(self):
        return self._service_name


if __name__ == '__main__':
    announcer = ServiceAnnouncer('Test Service', '_test._tcp', 12345, ['foo=bar', '42=true'])
    print announcer.get_service_name()

    sleep(42)

Using avahi-browse to verify it is indeed published:

micke@els-mifr-03:~$ avahi-browse -a -v -t -r 
Server version: avahi 0.6.30; Host name: els-mifr-03.local
E Ifce Prot Name                                          Type                 Domain
+   eth0 IPv4 Test Service                                  _test._tcp           local
=   eth0 IPv4 Test Service                                  _test._tcp           local
   hostname = [els-mifr-03.local]
   address = [10.9.0.153]
   port = [12345]
   txt = ["42=true" "foo=bar"]
like image 115
Micke Avatar answered Oct 04 '22 05:10

Micke


Avahi is "just" a Client implementation of ZeroConfig which basically is a "Multicast based DNS" protocol. You can use Avahi to publish the availability of your "data" through end-points. The actual data must be retrieved through some other means but you would normally register an end-point that can be "invoked" through a method of your liking.

like image 34
jldupont Avatar answered Oct 04 '22 04:10

jldupont