As discussed in the last post, I use the JmDNS library for service advertisement on the local area network. You can refer to it here. In this post, I’ll discuss how I used the pyZeroconf library to search for the service advertised by the android device. There do exist other libraries but the ones that I came across are dependent on avahi or bonjour and need them to be pre installed on the system and since I was working on py based api, I preferred not to have the extra baggage.
Here goes the sample code :
We could also use pyBonjour to search for devices from the browser by a simple hack, since the browsers generally have no native support for service discovery. The following code is just a proof of concept and uses an always running Simple HTTP Server to serve the results to a request from the browser.
Now, if you make an HTTP Request to http://127.0.0.1:7679/search (better if async) , it starts a zeroconf search for the services of the type ‘_dynamix._tcp.local.’ and provides the ip list as the response.
This can be made much more efficient (browsers might even have a persistent connection?), provide more useful data as the response but as a proof of concept, definitely works! Tested on Ubuntu 14.04 and Windows 7 :)