Skip to content
Related Articles

Related Articles

Python – Network Interface
  • Last Updated : 24 Nov, 2020

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:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :