frawau / aiozeroconf

An asyncio/pure python implementation of multicast DNS service discovery
GNU Lesser General Public License v2.1
25 stars 6 forks source link

python-aiozeroconf

.. image:: https://travis-ci.org/frawau/aiozeroconf.svg?branch=master :target: https://travis-ci.org/frawau/aiozeroconf

This is port of pyzeroconf to asyncio. It is based on pyzeroconf, Multicast DNS Service Discovery for Python, originally by Paul Scott-Murphy (https://github.com/paulsm/pyzeroconf), modified by William McBrine (https://github.com/wmcbrine/pyzeroconf). This port was written by François Wautier (https://github.com/frawau/pyzeroconf).

The original William McBrine's fork note::

This fork is used in all of my TiVo-related projects: HME for Python
(and therefore HME/VLC), Network Remote, Remote Proxy, and pyTivo.
Before this, I was tracking the changes for zeroconf.py in three
separate repos. I figured I should have an authoritative source.

Although I make changes based on my experience with TiVos, I expect that
they're generally applicable. This version also includes patches found
on the now-defunct (?) Launchpad repo of pyzeroconf, and elsewhere
around the net -- not always well-documented, sorry.

Compatible with:

Compared to some other Zeroconf/Bonjour/Avahi Python packages, python-zeroconf:

Python compatibility

Versioning

This project's versions follow the following pattern: MAJOR.MINOR.PATCH.

Status

It is the very beginning. I plan to use this in a project.

Compare to the original package, a number of method have become coroutines.

How to get aiozeroconf?

The easiest way to install aiozeroconf is using pip::

pip3 install aiozeroconf

How do I use it?

Here's an example of browsing for a service:

.. code-block:: python

import asyncio
from aiozeroconf import ServiceBrowser, Zeroconf

async def do_close(zc):
    await zc.close()

class MyListener(object):

    def remove_service(self, zeroconf, type_, name):
        print("Service %s removed" % (name,))

    def add_service(self, zeroconf, type_, name):
        asyncio.ensure_future(self.found_service(zeroconf, type_, name))

    async def found_service(self, zeroconf, type_, name):
        info = await zeroconf.get_service_info(type_, name)
        print("Adding {}".format(info))

loop = asyncio.get_event_loop()
zeroconf = Zeroconf(loop)
listener = MyListener()
browser = ServiceBrowser(zeroconf, "_http._tcp.local.", listener)
try:
    loop.run_forever()
except KeyboardInterrupt:
    print("Unregistering...")
    loop.run_until_complete(do_close(zeroconf))
finally:
    loop.close()

.. note::

Discovery and service registration use *all* available network interfaces by default
and both IPv4 and IPv6. If you want to customize that you need to specify:
    ``address_family``: a list containing netifaces.AF_NET and/or netiface.AF_INET6

    ``iface``: the name of the interface (e.g. "lo", "eth0")
when constructing ``Zeroconf`` object (see the code for details).

If you don't know the name of the service you need to browse for, try:

.. code-block:: python

import asyncio
from aiozeroconf import Zeroconf, ZeroconfServiceTypes

async def list_service(zc):
    los = await ZeroconfServiceTypes.find(zc,timeout=0.5)
    print ("Found {}".format(los))

loop = asyncio.get_event_loop()
zc = Zeroconf(loop)
loop.run_until_complete(list_service(zc))
loop.close()

See examples directory for more.

You can also run the module with::

python3 -m aiozeroconf -h

or directly::

aiozeroconf -h

Try -h for help

Changelog

0.1.6

0.1.0

^ First version

License

GPL, see COPYING file for details.