1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
#!/usr/bin/env @PYTHON@
# -*-python-*-
# $Id$
import avahi, dbus, gobject, sys
try:
import dbus.glib
except ImportError, e:
pass
service_type_browsers = {}
service_browsers = {}
def service_resolved(interface, protocol, name, type, domain, host, aprotocol, address, port, txt):
print "Service data for service '%s' of type '%s' in domain '%s' on %i.%i:" % (name, type, domain, interface, protocol)
print "\tHost %s (%s), port %i, TXT data: %s" % (host, address, port, str(txt))
def print_error(err):
print "Error:", str(err)
def new_service(interface, protocol, name, type, domain):
global server
print "Found service '%s' of type '%s' in domain '%s' on %i.%i." % (name, type, domain, interface, protocol)
# Asynchronous resolving
server.ResolveService(interface, protocol, name, type, domain, avahi.PROTO_UNSPEC, reply_handler=service_resolved, error_handler=print_error)
def remove_service(interface, protocol, name, type, domain):
print "Service '%s' of type '%s' in domain '%s' on %i.%i disappeared." % (name, type, domain, interface, protocol)
def new_service_type(interface, protocol, type, domain):
global server, service_browsers
# Are we already browsing this domain for this type?
if service_browsers.has_key((interface, protocol, type, domain)):
return
print "Browsing for services of type '%s' in domain '%s' on %i.%i ..." % (type, domain, interface, protocol)
b = dbus.Interface(bus.get_object(avahi.DBUS_NAME, server.ServiceBrowserNew(interface, protocol, type, domain)), avahi.DBUS_INTERFACE_SERVICE_BROWSER)
b.connect_to_signal('ItemNew', new_service)
b.connect_to_signal('ItemRemove', remove_service)
service_browsers[(interface, protocol, type, domain)] = b
def browse_domain(interface, protocol, domain):
global server, service_type_browsers
# Are we already browsing this domain?
if service_type_browsers.has_key((interface, protocol, domain)):
return
print "Browsing domain '%s' on %i.%i ..." % (domain, interface, protocol)
b = dbus.Interface(bus.get_object(avahi.DBUS_NAME, server.ServiceTypeBrowserNew(interface, protocol, domain)), avahi.DBUS_INTERFACE_SERVICE_TYPE_BROWSER)
b.connect_to_signal('ItemNew', new_service_type)
service_type_browsers[(interface, protocol, domain)] = b
def new_domain(interface, protocol, domain):
# We browse for .local anyway...
if domain != "local":
browse_domain(interface, protocol, domain)
domain = None
if len(sys.argv) > 1:
domain = sys.argv[1]
bus = dbus.SystemBus()
server = dbus.Interface(bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
if domain is None:
# Explicitly browse .local
browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "local")
# Browse for other browsable domains
db = dbus.Interface(bus.get_object(avahi.DBUS_NAME, server.DomainBrowserNew(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "", avahi.DOMAIN_BROWSER_BROWSE)), avahi.DBUS_INTERFACE_DOMAIN_BROWSER)
db.connect_to_signal('ItemNew', new_domain)
else:
# Just browse the domain the user wants us to browse
browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, domain)
try:
gobject.MainLoop().run()
except KeyboardInterrupt, k:
pass
|