-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscovery.py
More file actions
executable file
·59 lines (40 loc) · 1.52 KB
/
Copy pathdiscovery.py
File metadata and controls
executable file
·59 lines (40 loc) · 1.52 KB
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
import avahi
import dbus
class ServicePublisher(object):
def __init__(self, name, port=5000, svctype='_http._tcp', domain='', host='', msg=''):
self.group = None
self.name = name
self.port = port
self.svctype = svctype
self.domain = domain
self.host = host
self.msg = msg
def __enter__(self):
self.publish()
return self
def __exit__(self, a, b, c):
try:
self.unpublish()
except Exception as e:
print('exception in unpublish (%s)'.format(str(e)))
return False
def publish(self):
bus = dbus.SystemBus()
server = dbus.Interface(
bus.get_object(
avahi.DBUS_NAME,
avahi.DBUS_PATH_SERVER),
avahi.DBUS_INTERFACE_SERVER)
grp = dbus.Interface(
bus.get_object(avahi.DBUS_NAME,
server.EntryGroupNew()),
avahi.DBUS_INTERFACE_ENTRY_GROUP)
grp.AddService(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC,dbus.UInt32(0),
self.name, self.svctype, self.domain, self.host,
dbus.UInt16(self.port), self.msg)
grp.Commit()
self.group = grp
print('advertising avahi service')
def unpublish(self):
print('stop advertising avahi service')
self.group.Reset()