Python - Network Interface

Neha Kumawat

2 months ago

When we have multiple interfaces on a computer, we need to have their names, status, etc. In Python, we can list the interfaces and their state.

Checking all networking Interface in a computer

Here's a version that works on Python 3.4, both 32-bit and 64-bit, it returns both the interface name and its address, and as a bonus, it's not limited to 128 interfaces (I don't think it will really be a problem for anyone, though)
import socket
import fcntl
import struct
import array
import sys

def all_interfaces():
    is_64bits = sys.maxsize > 2**32
    struct_size = 40 if is_64bits else 32
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    max_possible = 8 # initial value
    while True:
        _bytes = max_possible * struct_size
        names = array.array('B')
        for i in range(0, _bytes):
            names.append(0)
        outbytes = struct.unpack('iL', fcntl.ioctl(
            s.fileno(),
            0x8912,  # SIOCGIFCONF
            struct.pack('iL', _bytes, names.buffer_info()[0])
        ))[0]
        if outbytes == _bytes:
            max_possible *= 2
        else:
            break
    namestr = names.tostring()
    ifaces = []
    for i in range(0, outbytes, struct_size):
        iface_name = bytes.decode(namestr[i:i+16]).split('\0', 1)[0]
        iface_addr = socket.inet_ntoa(namestr[i+20:i+24])
        ifaces.append((iface_name, iface_addr))

    return ifaces

Example

In the following, we use for example the Python module netifaces, which gives the details of the interfaces and their status. The methods used are very simple and straightforward.   

netifaces

  • A third party portable library in Python for listing network interfaces on the local computer.
  • In the past, it has been difficult to directly get the network address (es) of the computer where your Python scripts are running without affecting the portability of your script. * at least no systems) 
  • netifaces takes care of enumerating interfaces, network addresses and also preserves the portability(works on all *nix systems at least)

import netifaces

print (netifaces.interfaces())


print (netifaces.ifaddresses('lo'))

print (netifaces.AF_LINK)

addrs = netifaces.ifaddresses('ens33')
print(addrs[netifaces.AF_INET])


print(addrs[netifaces.AF_LINK])
If we run the above program, we get the following output :

# Result

['lo', 'ens33']
{17: [{'peer': '00:00:00:00:00:00', 'addr': '00:00:00:00:00:00'}], 2: [{'peer': '127.0.0.1', 'addr': '127.0.0.1', 'netmask': '255.0.0.0'}], 10: [{'addr': '::1', 'netmask': 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128'}]}

17

[{'netmask': '255.255.255.0', 'addr': '192.168.232.128', 'broadcast': '192.168.232.255'}]
[{'addr': '00:0c:29:ea:13:0a', 'broadcast': 'ff:ff:ff:ff:ff:ff'}]

      
I hope you enjoyed reading this article and finally, you came to know about Python Network Interface  .
For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Learning…  

Submit Review