| Ed Tanous | c9b5521 | 2017-06-12 13:25:51 -0700 | [diff] [blame] | 1 | # Copyright (c) Benjamin Kietzman (github.com/bkietz) | 
|  | 2 | # | 
|  | 3 | # Distributed under the Boost Software License, Version 1.0. (See accompanying | 
|  | 4 | # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | 
|  | 5 |  | 
|  | 6 | import unittest | 
|  | 7 | import dbus | 
|  | 8 | from dbus.mainloop.glib import DBusGMainLoop | 
|  | 9 | from gobject import MainLoop | 
|  | 10 | from socket import gethostname | 
|  | 11 |  | 
|  | 12 | class AvahiTest(unittest.TestCase): | 
|  | 13 |  | 
|  | 14 | @classmethod | 
|  | 15 | def setUpClass(c): | 
|  | 16 | c.system_bus = dbus.SystemBus(mainloop=DBusGMainLoop()) | 
|  | 17 |  | 
|  | 18 | def setUp(self): | 
|  | 19 | None | 
|  | 20 |  | 
|  | 21 | def testAvahi(self): | 
|  | 22 | # Connect to Avahi Daemon's interface: | 
|  | 23 | avahi_remote = AvahiTest.system_bus.get_object('org.freedesktop.Avahi', '/') | 
|  | 24 | avahi = dbus.Interface(avahi_remote, 'org.freedesktop.Avahi.Server') | 
|  | 25 | self.assertEqual(gethostname(), avahi.GetHostName()) | 
|  | 26 |  | 
|  | 27 | # Use the Avahi Daemon to produce a new | 
|  | 28 | # ServiceBrowser and connect to its interface: | 
|  | 29 | browser_path = avahi.ServiceBrowserNew(-1, -1, "_http._tcp", "local", dbus.UInt32(0)) | 
|  | 30 | browser_remote = AvahiTest.system_bus.get_object('org.freedesktop.Avahi', browser_path) | 
|  | 31 |  | 
|  | 32 | browser = dbus.Interface(browser_remote, 'org.freedesktop.Avahi.ServiceBrowser') | 
|  | 33 |  | 
|  | 34 | # Connect to the ItemNew signal from the browser: | 
|  | 35 | def new_item_handler(interface, protocol, instance_name, instance_type, domain, flags): | 
|  | 36 | print "Found service '%s'" % instance_name | 
|  | 37 |  | 
|  | 38 | browser.connect_to_signal("ItemNew", new_item_handler) | 
|  | 39 |  | 
|  | 40 | if __name__ == '__main__': | 
|  | 41 | unittest.main() | 
|  | 42 | MainLoop().run() |