def test_get_local_addresses(self): # mostly a tripwire test from roslib.network import get_local_addresses addrs = get_local_addresses() self.assert_(type(addrs) == list) self.assert_(len(addrs)) for a in addrs: self.assert_(type(a) == str) # should be unaffected os.environ['ROS_IP'] = 'bar' self.assertEquals(addrs, get_local_addresses()) os.environ['ROS_HOSTNAME'] = 'foo' self.assertEquals(addrs, get_local_addresses())
def __is_local(hostname): ''' Test the hostname whether it is local or not. Uses socket.gethostbyname(). ''' try: # If Python has ipv6 disabled but machine.address can be resolved somehow to an ipv6 address, then host[4][0] will be int machine_ips = [ host[4][0] for host in socket.getaddrinfo(hostname, 0, 0, 0, socket.SOL_TCP) if isinstance(host[4][0], str) ] except socket.gaierror: with _LOCK: rospy.logdebug("host::HOSTS_CACHE resolve %s failed" % hostname) HOSTS_CACHE[hostname] = False return False local_addresses = ['localhost'] + network.get_local_addresses() # check 127/8 and local addresses result = ([ ip for ip in machine_ips if (ip.startswith('127.') or ip == '::1') ] != []) or (set(machine_ips) & set(local_addresses) != set()) with _LOCK: rospy.logdebug("host::HOSTS_CACHE add %s:%s" % (hostname, result)) HOSTS_CACHE[hostname] = result return result
def __is_local(cls, hostname): try: machine_addr = socket.gethostbyname(hostname) except socket.gaierror: import traceback print traceback.format_exc() return False local_addresses = ['localhost'] + get_local_addresses() # check 127/8 and local addresses result = machine_addr.startswith('127.') or machine_addr in local_addresses return result
def is_local(hostname, wait=True): ''' Test whether the given host name is the name of the local host or not. :param str hostname: the name or IP of the host :return: `True` if the hostname is local or None :rtype: bool :raise Exception: on errors while resolving host ''' if not hostname: return True with _LOCK: if hostname in HOSTS_CACHE: if isinstance(HOSTS_CACHE[hostname], threading.Thread): return False return HOSTS_CACHE[hostname] try: socket.inet_aton(hostname) local_addresses = ['localhost'] + network.get_local_addresses() # check 127/8 and local addresses result = hostname.startswith( '127.') or hostname == '::1' or hostname in local_addresses with _LOCK: rospy.logdebug("host::HOSTS_CACHE add local %s:%s" % (hostname, result)) HOSTS_CACHE[hostname] = result return result except socket.error: # the hostname must be resolved => do it in a thread if wait: result = __is_local(hostname) return result else: thread = threading.Thread(target=__is_local, args=((hostname, ))) thread.daemon = True with _LOCK: HOSTS_CACHE[hostname] = thread thread.start() return False
def is_local(cls, hostname, wait=False): ''' Test whether the given host name is the name of the local host or not. @param hostname: the name or IP of the host @type hostname: C{str} @return: C{True} if the hostname is local or None @rtype: C{bool} @raise Exception: on errors while resolving host ''' if (hostname is None): return True try: socket.inet_aton(hostname) local_addresses = ['localhost'] + get_local_addresses() # check 127/8 and local addresses result = hostname.startswith('127.') or hostname in local_addresses return result except socket.error: # the hostname must be resolved => do it in a thread if wait: result = cls.__is_local(hostname) return result return False