def testGetIPLoopback(self): # Just check loopback ifaces = tryto_detect_networks(no_loopback=False) loopback = False for i in ifaces: if i.ipv4_addr == "127.0.0.1": loopback = True break self.assert_(loopback==True, "Failed because loopback doesn't exists!")
def testGetIP(self): ifaces = tryto_detect_networks() # No Loopback # Get Real IP address import subprocess import tempfile # Create temporary files stdout_output = tempfile.NamedTemporaryFile(delete=False) stderr_output = tempfile.NamedTemporaryFile(delete=False) command = "ifconfig" # POSIX if os.name == "nt": command = "ipconfig" # Windows shell_state = (sys.platform == "win32") p = subprocess.Popen(command, bufsize= 1, stdin=subprocess.PIPE, stdout=stdout_output.fileno(), stderr=stderr_output.fileno(), shell=shell_state) p.wait() # Read contents of normal_desc = open(stdout_output.name, "r") normal = normal_desc.read() normal_desc.close() # Now look for ips: for i in ifaces: found = normal.rfind(i.ipv4_addr) self.assert_(found != -1, "Failing: IP %s not found" % i.ipv4_addr) # Remove Temporary Files stderr_output.close() os.remove(stderr_output.name) stdout_output.close() os.remove(stdout_output.name)