single |
netifaces 0.10.8
================
+-------------+------------------+
| Linux/macOS | |BuildStatus| |
+-------------+------------------+
| Windows | |WinBuildStatus| |
+-------------+------------------+
:alt: Build Status (Linux/Mac)
:alt: Build Status (Windows)
.. warning::
netifaces needs a new maintainer. al45tair is no longer able to
maintain it
or make new releases due to work commitments.
1. What is this?
----------------
It's been annoying me for some time that there's no easy way to get the
address(es) of the machine's network interfaces from Python. There is
a good reason for this difficulty, which is that it is virtually impossible
to do so in a portable manner. However, it seems to me that there should
be a package you can easy_install that will take care of working out the
details of doing so on the machine you're using, then you can get on with
writing Python code without concerning yourself with the nitty gritty of
system-dependent low-level networking APIs.
This package attempts to solve that problem.
2. How do I use it?
-------------------
First you need to install it, which you can do by typing::
tar xvzf netifaces-0.10.8.tar.gz
cd netifaces-0.10.8
python setup.py install
**Note that you will need the relevant developer tools for your platform**,
as netifaces is written in C and installing this way will compile the
extension.
Once that's done, you'll need to start Python and do something like the
following::
>>> import netifaces
Then if you enter
>>> netifaces.interfaces()
['lo0', 'gif0', 'stf0', 'en0', 'en1', 'fw0']
you'll see the list of interface identifiers for your machine.
You can ask for the addresses of a particular interface by doing
>>> netifaces.ifaddresses('lo0')
{18: [{'addr': ''}], 2: [{'peer': '127.0.0.1', 'netmask': '255.0.0.0',
'addr': '127.0.0.1'}], 30: [{'peer': '::1', 'netmask':
'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', 'addr': '::1'}, {'peer': '',
'netmask': 'ffff:ffff:ffff:ffff::', 'addr': 'fe80::1%lo0'}]}
Hmmmm. That result looks a bit cryptic; let's break it apart and explain
what each piece means. It returned a dictionary, so let's look there
first::
{ 18: [...], 2: [...], 30: [...] }
Each of the numbers refers to a particular address family. In this case,
we
have three address families listed; on my system, 18 is AF_LINK (which
means
the link layer interface, e.g. Ethernet), 2 is AF_INET (normal Internet
addresses), and 30 is AF_INET6 (IPv6).
But wait! Don't use these numbers in your code. The numeric values here
are
system dependent; fortunately, I thought of that when writing netifaces, so
the module declares a range of values that you might need. e.g.
>>> netifaces.AF_LINK
18
Again, on your system, the number may be different.
So, what we've established is that the dictionary that's returned has one
entry for each address family for which this interface has an address.
Let's
take a look at the AF_INET addresses now:
>>> addrs = netifaces.ifaddresses('lo0')
>>> addrs[netifaces.AF_INET]
[{'peer': '127.0.0.1', 'netmask': '255.0.0.0', 'addr': '127.0.0.1'}]
You might be wondering why this value is a list. The reason is that it's
possible for an interface to have more than one address, even within the
|