创建一个由 avahi 播放的节目

发布于 2024-08-06 20:12:37 字数 553 浏览 3 评论 0原文

我正在尝试编写一个程序,输出可以通过 avahi 网络提供的数据。我看过的文档似乎说我必须使用 dbus 注册服务,然后将其连接到 avahi,但执行此操作的文档非常稀疏。有谁知道它的良好文档吗?我一直在看这些:

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

我真的不熟悉 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无法言说的痛 2024-08-13 20:12:37

我意识到这个答案已经很晚了,考虑到你的问题是四年前提出的。但是,它可能对其他人有帮助。

下面公布了一个使用 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)

使用 avahi-browse 来验证它确实已发布:

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"]

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"]
貪欢 2024-08-13 20:12:37

Avahi“只是”ZeroConfig 的客户端实现,它基本上是“基于组播的 DNS”协议。您可以使用 Avahi 通过端点发布“数据”的可用性。实际数据必须通过其他方式检索,但您通常会注册一个可以通过您喜欢的方法“调用”的端点。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文