Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

95 lines (75 sloc) 2.878 kB
import select
import socket
import sys
import pybonjour
regtype = sys.argv[1]
timeout = 5
queried = []
resolved = []
def query_record_callback(sdRef, flags, interfaceIndex, errorCode, fullname,
rrtype, rrclass, rdata, ttl):
if errorCode == pybonjour.kDNSServiceErr_NoError:
print ' IP =', socket.inet_ntoa(rdata)
queried.append(True)
def resolve_callback(sdRef, flags, interfaceIndex, errorCode, fullname,
hosttarget, port, txtRecord):
if errorCode != pybonjour.kDNSServiceErr_NoError:
return
print 'Resolved service:'
print ' fullname =', fullname
print ' hosttarget =', hosttarget
print ' port =', port
query_sdRef = \
pybonjour.DNSServiceQueryRecord(interfaceIndex = interfaceIndex,
fullname = hosttarget,
rrtype = pybonjour.kDNSServiceType_A,
callBack = query_record_callback)
try:
while not queried:
ready = select.select([query_sdRef], [], [], timeout)
if query_sdRef not in ready[0]:
print 'Query record timed out'
break
pybonjour.DNSServiceProcessResult(query_sdRef)
else:
queried.pop()
finally:
query_sdRef.close()
resolved.append(True)
def browse_callback(sdRef, flags, interfaceIndex, errorCode, serviceName,
regtype, replyDomain):
if errorCode != pybonjour.kDNSServiceErr_NoError:
return
if not (flags & pybonjour.kDNSServiceFlagsAdd):
print 'Service removed'
return
print 'Service added; resolving'
resolve_sdRef = pybonjour.DNSServiceResolve(0,
interfaceIndex,
serviceName,
regtype,
replyDomain,
resolve_callback)
try:
while not resolved:
ready = select.select([resolve_sdRef], [], [], timeout)
if resolve_sdRef not in ready[0]:
print 'Resolve timed out'
break
pybonjour.DNSServiceProcessResult(resolve_sdRef)
else:
resolved.pop()
finally:
resolve_sdRef.close()
browse_sdRef = pybonjour.DNSServiceBrowse(regtype = regtype,
callBack = browse_callback)
try:
try:
while True:
ready = select.select([browse_sdRef], [], [])
if browse_sdRef in ready[0]:
pybonjour.DNSServiceProcessResult(browse_sdRef)
except KeyboardInterrupt:
pass
finally:
browse_sdRef.close()
Jump to Line
Something went wrong with that request. Please try again.