#!/usr/bin/env python"""This script using NSNetServiceBrowser to look for local HTTP servers."""importobjcfromFoundationimport*classPrintingResolverDelegate(NSObject):defnetServiceDidResolveAddress_(self,service):addresses=service.addresses()iflen(addresses)==0:returnprint"%s.%s"%(service.name(),service.domain())foraddressinservice.addresses():print" %s"%(address,)print""defnetService_didNotResolve_(self,didNotResolve):print"didNotResolve",didNotResolveclassPrintingBrowserDelegate(NSObject):defstartLookup(self):foraNetServiceinself.services:prd=PrintingResolverDelegate.new()aNetService.setDelegate_(prd)aNetService.resolve()defnetServiceBrowserWillSearch_(self,browser):print"Browsing for advertised services..."self.services=[]defnetServiceBrowserDidStopSearch_(self,browser):print"Browse complete"self.startLookup()defnetServiceBrowser_didNotSearch_(self,browser,errorDict):print"Could not search."defnetServiceBrowser_didFindService_moreComing_(self,browser,aNetService,moreComing):print"Found a service: %s%s"%(aNetService.name(),aNetService.domain())self.services.append(aNetService)ifnotmoreComing:browser.stop()defnetServiceBrowser_didRemoveService_moreComing_(self,browser,aNetService,moreComing):print"Service removed: %s"%(aNetService.name(),)ifnotmoreComing:browser.stop()deffindDomains(serviceName,seconds=5.0):runloop=NSRunLoop.currentRunLoop()browser=NSNetServiceBrowser.new()pbd=PrintingBrowserDelegate.new()browser.setDelegate_(pbd)browser.searchForServicesOfType_inDomain_(serviceName,"")untilWhen=NSDate.dateWithTimeIntervalSinceNow_(seconds)runloop.runUntilDate_(untilWhen)if__name__=='__main__':# Use '_afpovertcp' instead of '_http' to look for fileservers.findDomains("_http._tcp")