resolve.py 1.46 KB
Newer Older
O'Reilly Media, Inc. committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#!/usr/bin/python
#
# Bonjour resolving sample
# 
# Usage: python resolve.py <serviceName> <serviceType> <serviceDomain>
#
# e.g. python resolve.py "The Name" _http._tcp

import sys
import bonjour
import select

# Callback for service resolving
def ResolveCallback(sdRef, flags, interfaceIndex,
                    errorCode, fullname, hosttarget,
                    port, txtLen, txtRecord, userdata):
    print "Service:", fullname
    print "is at", hosttarget, ":", port

if len(sys.argv) < 4:
    print "Usage: resolve.py serviceName serviceType serviceDomain"
    sys.exit(1)

serviceName   = sys.argv[1]
serviceType   = sys.argv[2]
serviceDomain = sys.argv[3]

# Allocate a service discovery ref and resolve the named service
serviceRef = bonjour.AllocateDNSServiceRef()
ret = bonjour.pyDNSServiceResolve(serviceRef,
                                  0,
                                  0,
                                  serviceName,
                                  serviceType,
                                  serviceDomain,
                                  ResolveCallback,
                                  None);

if ret != bonjour.kDNSServiceErr_NoError:
    print "ret = %d; exiting" % ret
    sys.exit(1)

# Get socket descriptor and loop
fd = bonjour.DNSServiceRefSockFD(serviceRef)
while 1:
    ret = select.select([fd], [], [])
    ret = bonjour.DNSServiceProcessResult(serviceRef)

# Deallocate the service discovery ref
bonjour.DNSServiceRefDeallocate(serviceRef)