/usr/share/pyshared/maasserver/zeroconfservice.py is in python-django-maas 1.2+bzr1373+dfsg-0ubuntu1~12.04.6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # Copyright 2008 (c) Pierre Duquesne <stackp@online.fr>
# Copyright 2012 Canonical Ltd.
# This software is licensed under the GNU Affero General Public License
# version 3 (see the file LICENSE).
# Example code taken from http://stackp.online.fr/?p=35
"""Work with Zeroconf."""
from __future__ import (
absolute_import,
print_function,
unicode_literals,
)
__metaclass__ = type
__all__ = [
"ZeroconfService",
]
import avahi
import dbus
class ZeroconfService:
"""Publish a network service with Zeroconf using Avahi."""
def __init__(self, name, port, stype="_http._tcp",
domain="", host="", text=""):
"""Create an object that can publish a service over Avahi.
:param name: The name of the service to be published.
:param port: The port number where it's published.
:param stype: The service type string.
"""
self.name = name
self.stype = stype
self.domain = domain
self.host = host
self.port = port
self.text = text
def publish(self):
"""Publish the service through Avahi."""
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)
group.AddService(
avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, dbus.UInt32(0),
self.name, self.stype, self.domain, self.host,
dbus.UInt16(self.port), self.text)
group.Commit()
self.group = group
def unpublish(self):
"""Unpublish the service through Avahi."""
self.group.Reset()
if __name__ == "__main__":
service = ZeroconfService(name="TestService", port=3000)
service.publish()
raw_input("Press any key to unpublish the service ")
service.unpublish()
|