Python modules you should know: Netifaces

April 21, 2012 at 08:35 AM | categories: Python, PyMYSK, Howto | View Comments

We all know of the great python packages, Django, Sqlalchemy, Pylons etc, however there are so many unsung heroes that we get to use when we need to get something we don't do often done. This series will try and unearth those gems such that the next time you need to do something in Python, you know which module to use.

I am starting the series with the Netifaces package which i recently discovered, and it is the inspiration for this series.

Home page

Use

Portable access to network interfaces from Python, this module allows you to get information about network interfaces on a machine and it is portable across platforms

If you are writing any kind of software that should expose the network interface information to users this module will come in handy.

Installation

pip install netifaces

Usage

Get interfaces on the machine.

import netifaces
netifaces.interfaces()

Output

['lo0', 'gif0', 'stf0', 'pflog0', 'en0', 'en1', 'fw0', 'p2p0']

Get an interfaces addresses

import netifaces
netifaces.ifaddresses('en1')

Output

{18: [{'addr': '68:a8:6d:28:67:9e'}],
2: [{'broadcast': '192.168.1.255', 'netmask': '255.255.255.0', 'addr': '192.168.1.23'}],
30: [{'netmask': 'ffff:ffff:ffff:ffff::', 'addr': 'fe80::6aa8:6dff:fe28:679e%en1'}]}

blog comments powered by Disqus