Python – Network Interface
Network interface is nothing but interconnection between two hardware equipment or protocol layers in a computer network. A network interface usually has some form of the network address. It is generally a network interface card that does not have any physical existence. It can be implemented in a software interface.
It is difficult to keep track of interface names, status, or any other information related to them when we have multiple interfaces. For this purpose, Python has a library called netifaces which can list the interfaces and their status. The netifaces module is a portable third-party library which enumerates the network interfaces on the local machine. Below is a simple example using python netifaces module giving details of interfaces and their status.
Installation:
pip install netifaces
Implementation of netifaces module for various network operations:
Python3
# Import librariesimport netifaces # Showing gateway listnetifaces.gateways() # Getting interfacesinterfaces = netifaces.interfaces() # Showing interfacesfor interface in interfaces: print(interface) # Getting interface infoprint(netifaces.ifaddresses(str(interfaces[0]))) # Getting interface statusaddrs = netifaces.ifaddresses(str(interfaces[0]))print(addrs[netifaces.AF_INET])print(addrs[netifaces.AF_LINK]) |
Output:




