Rate This Document
Findability
Accuracy
Completeness
Readability

Network Information

Use the netifaces module to obtain information such as the local IP address and gateway.

Example:

def GetNetworkIP():
# Obtains the IP address of the local NIC.
    import netifaces
#routingGateway = netifaces.gateways()['default'][netifaces.AF_INET][0] # Gateway
routingNicName = netifaces.gateways()['default'][netifaces.AF_INET][1] # Network adapter information
 
    for interface in netifaces.interfaces():
        if interface == routingNicName:
            #print (netifaces.ifaddresses(interface))
            try:
routingIPAddr = netifaces.ifaddresses(interface)[netifaces.AF_INET][0]['addr'] # Obtains the IP address
            except KeyError:
                pass
    #print ("Routing IP Address:%s"% routingIPAddr)
    return routingIPAddr
if __name__ == "__main__":
    try:
        print ("Routing IP Address:",GetNetworkIP())
    except:
        print ("Unable to get the address, there may not be installed netifaces module! command: pip install netifaces")