Skip to content

Instantly share code, notes, and snippets.

@vshivam
Created February 2, 2015 15:57
Show Gist options
  • Save vshivam/8b291abadedaa8b0bcba to your computer and use it in GitHub Desktop.
Save vshivam/8b291abadedaa8b0bcba to your computer and use it in GitHub Desktop.
Simple HttpServer which serves DNS-SD Service info
"""
Serves files out of its current directory.
Doesn't handle POST requests.
"""
import SocketServer
import SimpleHTTPServer
from zeroconf import *
import socket
import time
PORT = 7678
devices = [];
class ZeroconfListener(object):
def __init__(self):
self.r = Zeroconf()
def removeService(self, zeroconf, type, name):
print
print "Service", name, "removed"
def addService(self, zeroconf, type, name):
print
print "Service", name, "added"
print " Type is", type
info = self.r.getServiceInfo(type, name)
if info:
devices.append(socket.inet_ntoa(info.getAddress()))
print " Address is %s:%d" % (socket.inet_ntoa(info.getAddress()),
info.getPort())
print " Weight is %d, Priority is %d" % (info.getWeight(),
info.getPriority())
print " Server is", info.getServer()
prop = info.getProperties()
if prop:
print " Properties are"
for key, value in prop.items():
print " %s: %s" % (key, value)
def searchForDynamixServices():
r = Zeroconf()
print "Testing browsing for a service..."
type = "_dynamix._tcp.local."
listener = ZeroconfListener()
browser = ServiceBrowser(r, type, listener)
time.sleep(5)
r.close()
return devices[0]
class CustomHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
if self.path=='/search':
#This URL will trigger our sample function and send what it returns back to the browser
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
self.wfile.write(searchForDynamixServices()) #call sample function here
return
else:
#serve files, and directory listings by following self.path from
#current working directory
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
httpd = SocketServer.ThreadingTCPServer(('localhost', PORT),CustomHandler)
print "serving at port", PORT
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment