Open In App

Python – Network Interface

Improve
Improve
Like Article
Like
Save
Share
Report

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 libraries
import netifaces
  
# Showing gateway list
netifaces.gateways()
  
# Getting interfaces
interfaces = netifaces.interfaces()
  
# Showing interfaces
for interface in interfaces:
    print(interface)
  
# Getting interface info
print(netifaces.ifaddresses(str(interfaces[0])))
  
# Getting interface status
addrs = netifaces.ifaddresses(str(interfaces[0]))
print(addrs[netifaces.AF_INET])
print(addrs[netifaces.AF_LINK])


Output:


Last Updated : 24 Nov, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads