【问题标题】:Stopping the Avahi service and return a list of elements停止 Avahi 服务并返回元素列表
【发布时间】:2011-07-04 19:51:54
【问题描述】:

this example 之后,我想在获取所有元素并将它们返回列表时停止服务。

停止无限循环是调用gobject.MainLoop().quit(),但不知道何时何地调用。 谢谢

【问题讨论】:

    标签: python dbus avahi


    【解决方案1】:

    当最后一个服务到达时,您需要停止服务浏览。这将为您提供当前可用服务的快照。

    C 层中有一个标志,可让您检查是否有更多服务即将推出。它被称为AVAHI_BROWSER_ALL_FOR_NOW。我不熟悉 python 绑定,但也许可以在回调的 stypeflags 参数中以某种方式检查此标志?

    当你看到这个标志被设置时 - 打电话给gobject.MainLoop().quit()

    【讨论】:

      【解决方案2】:

      您还需要收听 AllForNow 信号。考虑 CacheExhaustedFailureorg.freedesktop.Avahi.ServiceBrowser 的接口声明为here

      在我的机器上运行以下命令会在大约一秒钟后返回并打印:

      browsing ...
      cache exhausted
      New service: name = Lexmark X264dn; address = 10.9.0.93; port = 80
      that's all for now
      done
      0:00:01.004258
      

      希望对你有帮助!


      from _dbus_glib_bindings import DBusGMainLoop
      import dbus, avahi
      import gobject
      
      class ServiceBrowser():
          def __init__(self, service):
              loop = DBusGMainLoop(set_as_default=True)
      
              bus = dbus.SystemBus(mainloop=loop)
      
              self._server = dbus.Interface(bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
      
              browser = self._server.ServiceBrowserNew(avahi.IF_UNSPEC, avahi.PROTO_INET, service, '', 0)
              listener = dbus.Interface(bus.get_object(avahi.DBUS_NAME, browser), avahi.DBUS_INTERFACE_SERVICE_BROWSER)
      
              listener.connect_to_signal("ItemNew", self.item_new_handler)
              listener.connect_to_signal("ItemRemove", self.item_remove_handler)
              listener.connect_to_signal("AllForNow", self.all_for_now_handler)
              listener.connect_to_signal("CacheExhausted", self.cache_exhausted_handler)
              listener.connect_to_signal("Failure", self.failure_handler)
      
              self._mainloop = gobject.MainLoop()
      
      
          def service_resolved(self, *args):
              print 'New service: name = %s; address = %s; port = %s' % (args[2], args[7], args[8])
      
      
          def print_error(self, *args):
              print 'error_handler'
              print args[0]
      
          def item_new_handler(self, interface, protocol, name, stype, domain, flags):
              self._server.ResolveService(interface, protocol, name, stype, domain, avahi.PROTO_UNSPEC, dbus.UInt32(0), reply_handler=self.service_resolved, error_handler=self.print_error)
      
      
          def item_remove_handler(self, interface, protocol, name, stype, domain, flags):
              print "Removed service: %s" % name
      
      
          def all_for_now_handler(self):
              print "that's all for now"
              self._mainloop.quit()
      
      
          def cache_exhausted_handler(self):
              print "cache exhausted"
      
      
          def failure_handler(self, error):
              print "failure: %s" % error
      
      
          def browse(self):
              self._mainloop.run()
      
      
      def browse_avahi(service):
          browser = ServiceBrowser(service)
          browser.browse()
      
      def main():
          t0 = datetime.utcnow()
          print "browsing ..."
          browse_avahi('_http._tcp')
          print "done"
          print datetime.utcnow()-t0
      
      if __name__ == '__main__':
          main()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-19
        相关资源
        最近更新 更多