Creazione di un programma da trasmettere da avahi
Domanda
Sto cercando di scrivere un programma che genera dati che possono essere serviti su una rete con Avahi. La documentazione che ho guardato sembra dire che devo registrare il servizio con dbus e poi collegarlo al avahi, ma la documentazione per fare questo è piuttosto scarsa. Qualcuno sa di una buona documentazione per questo? Ho cercato in questi:
python-dbus: http://dbus.freedesktop.org/doc/dbus -python / doc / tutorial.html # esportazione-oggetti
python-avahi: http://www.amk.ca/diary/2007/04/rough_notes_python_and_dbus.html
Sono molto familiarità con il modo avahi funziona a tutti, quindi tutti i puntatori sarebbe utile.
Soluzione
Avahi è "solo" un'implementazione client di ZeroConfig che fondamentalmente si basa un "Multicast DNS protocollo". È possibile utilizzare Avahi di pubblicare la disponibilità del "dati" attraverso end-point. I dati effettivi devono essere recuperati tramite altri mezzi, ma è normalmente registrare un end-point che possono essere "invocato" attraverso un metodo di vostro gradimento.
Altri suggerimenti
Mi rendo conto che questa risposta è piuttosto tardi, nel considerare la propria domanda è stato chiesto quattro anni fa. Tuttavia, potrebbe aiutare gli altri.
Il seguente annuncia un servizio utilizzando 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)
Utilizzando avahi-passi in rassegna per verificare che è infatti pubblicato:
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"]