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 netifaces
netifaces.gateways()
interfaces = netifaces.interfaces()
for interface in interfaces:
print (interface)
print (netifaces.ifaddresses( str (interfaces[ 0 ])))
addrs = netifaces.ifaddresses( str (interfaces[ 0 ]))
print (addrs[netifaces.AF_INET])
print (addrs[netifaces.AF_LINK])
|
Output:
