Giter VIP home page Giter VIP logo

zeroconf-python-client-service-example's Introduction

zeroconf Python service & client example

Demonstrates how to build a simple service that will be discovered via a Python script using zeroconf.

The service is a very simple API that responds with a tiny block of JSON. It's configured to run with the name simpleapi (defined in simpleapi.service).

The main client will query the network for any services and discover implementations of the service (based on it's name). Based on the discovered services it will make a request to each to obtain the simple JSON block.

Getting started

Start the docker image(s):

sudo docker run --net=host -it --rm antillionos/zeroconf-example-service

Install the required Python dependencies:

pip install -r client/requirements.txt

Discover the available services using the following command (you may receive a firewall notification as it will also be listening on the socket):

python client/find_and_call.py

Assuming all goes well, you should see something like the following:

Waiting 10s for services to become available (Ctrl+C to cancel search early)

Service developmentbox simpleapi._http._tcp.local. of type _http._tcp.local. state changed: ServiceStateChange.Added
Discovered a service: {u'path': '/api/v1', u'port': 8000, u'address': '11.0.0.11'}

Querying 1 services found
Service @ 11.0.0.11 responded with 200:{"Hello": "World!"} 

Troubleshooting

In the service

If you see:

Mar  7 14:44:20 b0b7383106e2 avahi-daemon[70]: sendmsg() to 0:0:ff02:: failed: Invalid argument

Avahi is failing to broadcast to mDNS; make sure you started the container with --net=host.

On the client

If you can't see your service come up, try client/browser.py which will show all services it can find.

Pass the --debug flag if you wish to see the mDNS network traffic; be warned: it's noisy.

Credits

The service Dockerfile is based, in part, on the work from https://github.com/mhaberler/docker-avahi which solves common dbus problems.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.