Skip to main content
Build Status

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.6.tar.gz
cd netifaces-0.10.6
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 same family. I’ll say that again: you can have more than one address of the same type associated with each interface.

Asking for “the” address of a particular interface doesn’t make sense.

Right, so, we can see that this particular interface only has one address, and, because it’s a loopback interface, it’s point-to-point and therefore has a peer address rather than a broadcast address.

Let’s look at a more interesting interface.

>>> addrs = netifaces.ifaddresses('en0')
>>> addrs[netifaces.AF_INET]
[{'broadcast': '10.15.255.255', 'netmask': '255.240.0.0', 'addr': '10.0.1.4'}, {'broadcast': '192.168.0.255', 'addr': '192.168.0.47'}]

This interface has two addresses (see, I told you…) Both of them are regular IPv4 addresses, although in one case the netmask has been changed from its default. The netmask may not appear on your system if it’s set to the default for the address range.

Because this interface isn’t point-to-point, it also has broadcast addresses.

Now, say we want, instead of the IP addresses, to get the MAC address; that is, the hardware address of the Ethernet adapter running this interface. We can do

>>> addrs[netifaces.AF_LINK]
[{'addr': '00:12:34:56:78:9a'}]

Note that this may not be available on platforms without getifaddrs(), unless they happen to implement SIOCGIFHWADDR. Note also that you just get the address; it’s unlikely that you’ll see anything else with an AF_LINK address. Oh, and don’t assume that all AF_LINK addresses are Ethernet; you might, for instance, be on a Mac, in which case:

>>> addrs = netifaces.ifaddresses('fw0')
>>> addrs[netifaces.AF_LINK]
[{'addr': '00:12:34:56:78:9a:bc:de'}]

No, that isn’t an exceptionally long Ethernet MAC address—it’s a FireWire address.

As of version 0.10.0, you can also obtain a list of gateways on your machine:

>>> netifaces.gateways()
{2: [('10.0.1.1', 'en0', True), ('10.2.1.1', 'en1', False)], 30: [('fe80::1', 'en0', True)], 'default': { 2: ('10.0.1.1', 'en0'), 30: ('fe80::1', 'en0') }}

This dictionary is keyed on address family—in this case, AF_INET—and each entry is a list of gateways as (address, interface, is_default) tuples. Notice that here we have two separate gateways for IPv4 (AF_INET); some operating systems support configurations like this and can either route packets based on their source, or based on administratively configured routing tables.

For convenience, we also allow you to index the dictionary with the special value 'default', which returns a dictionary mapping address families to the default gateway in each case. Thus you can get the default IPv4 gateway with

>>> gws = netifaces.gateways()
>>> gws['default'][netifaces.AF_INET]
('10.0.1.1', 'en0')

Do note that there may be no default gateway for any given address family; this is currently very common for IPv6 and much less common for IPv4 but it can happen even for AF_INET.

BTW, if you’re trying to configure your machine to have multiple gateways for the same address family, it’s a very good idea to check the documentation for your operating system very carefully, as some systems become extremely confused or route packets in a non-obvious manner.

I’m very interested in hearing from anyone (on any platform) for whom the gateways() method doesn’t produce the expected results. It’s quite complicated extracting this information from the operating system (whichever operating system we’re talking about), and so I expect there’s at least one system out there where this just won’t work.

3. This is great! What platforms does it work on?

It gets regular testing on OS X, Linux and Windows. It has also been used successfully on Solaris, and it’s expected to work properly on other UNIX-like systems as well. If you are running something that is not supported, and wish to contribute a patch, please use BitBucket to send a pull request.

4. What license is this under?

It’s an MIT-style license. Here goes:

Copyright (c) 2007-2017 Alastair Houghton

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

5. Why the jump to 0.10.0?

Because someone released a fork of netifaces with the version 0.9.0. Hopefully skipping the version number should remove any confusion. In addition starting with 0.10.0 Python 3 is now supported and other features/bugfixes have been included as well. See the CHANGELOG for a more complete list of changes.

Metadata

Release files for netifaces 0.10.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for netifaces 0.10.6
File Size Uploaded
netifaces-0.10.6.tar.gz 25.2 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for netifaces 0.10.6
File
netifaces-0.10.6.win-amd64-py3.5.exe Details
netifaces-0.10.6.win-amd64-py3.4.exe Details
netifaces-0.10.6.win-amd64-py2.7.exe Details
netifaces-0.10.6.win32-py3.5.exe Details
netifaces-0.10.6.win32-py3.4.exe Details
netifaces-0.10.6.win32-py2.7.exe Details
netifaces-0.10.6-py3.5-win-amd64.egg Legacy Egg format - - Details
netifaces-0.10.6-py3.5-win32.egg Legacy Egg format - - Details
netifaces-0.10.6-py3.4-win-amd64.egg Legacy Egg format - - Details
netifaces-0.10.6-py3.4-win32.egg Legacy Egg format - - Details
netifaces-0.10.6-py2.7-win-amd64.egg Legacy Egg format - - Details
netifaces-0.10.6-py2.7-win32.egg Legacy Egg format - - Details
netifaces-0.10.6-cp35-cp35m-win_amd64.whl CPython 3.5 CPython 3.5 pymalloc Windows x86-64 Details
netifaces-0.10.6-cp35-cp35m-win32.whl CPython 3.5 CPython 3.5 pymalloc Windows x86-32 Details
netifaces-0.10.6-cp34-cp34m-win_amd64.whl CPython 3.4 CPython 3.4 pymalloc Windows x86-64 Details
netifaces-0.10.6-cp34-cp34m-win32.whl CPython 3.4 CPython 3.4 pymalloc Windows x86-32 Details
netifaces-0.10.6-cp27-cp27m-win_amd64.whl CPython 2.7 CPython 2.7 pymalloc Windows x86-64 Details
netifaces-0.10.6-cp27-cp27m-win32.whl CPython 2.7 CPython 2.7 pymalloc Windows x86-32 Details

Total release size: 2.3 MB

Release files / netifaces-0.10.6.tar.gz

Download URL netifaces-0.10.6.tar.gz
Size 25.2 kB
Tags Source
SHA-256 checksum
How to use checksums
0c4da523f36d36f1ef92ee183f2512f3ceb9a9d2a45f7d19cda5a42c6689ebe0
BLAKE2b-256 checksum
How to use checksums
7201ba076082628901bca750bf53b322a8ff10c1d757dc29196a8e6082711c9d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / netifaces-0.10.6.win-amd64-py3.5.exe

Download URL netifaces-0.10.6.win-amd64-py3.5.exe
Size 615.2 kB
Tags Source
SHA-256 checksum
How to use checksums
369eb616a6c844987bd4df486bb5f591aa0d5552378c6831f56ed81cfc228cab
BLAKE2b-256 checksum
How to use checksums
1fb1ef234e36bff301160be80130f0d609909573aa36707da8dcef30b3e14898
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / netifaces-0.10.6.win-amd64-py3.4.exe

Download URL netifaces-0.10.6.win-amd64-py3.4.exe
Size 246.6 kB
Tags Source
SHA-256 checksum
How to use checksums
c455ca29737bf9b298408fd78a48f8fc6ddaa1f50a6eb92d814a8874412c631b
BLAKE2b-256 checksum
How to use checksums
3fd88199028c646f31b80177d8e38da61384985a103a6ea69d2d6ad0691bc36b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / netifaces-0.10.6.win-amd64-py2.7.exe

Download URL netifaces-0.10.6.win-amd64-py2.7.exe
Size 246.5 kB
Tags Source
SHA-256 checksum
How to use checksums
60f25e5b6d2a682a394c87a6d2bf4d38c8dd8999ee32b955af88ceccaef7fe93
BLAKE2b-256 checksum
How to use checksums
3a49e8f4973c623cf03e46fc10e6c0b48c93b5b8c231a25aa616d09b2d198ad4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / netifaces-0.10.6.win32-py3.5.exe

Download URL netifaces-0.10.6.win32-py3.5.exe
Size 484.4 kB
Tags Source
SHA-256 checksum
How to use checksums
88d8fa4fcccaca07519141e95b42f52fb650bed2e8f5b29c44e22968b92b7097
BLAKE2b-256 checksum
How to use checksums
b8fbc696726248db75c16cdeddd87f8a555d3a96208e937ef7bbfc301f833359
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / netifaces-0.10.6.win32-py3.4.exe

Download URL netifaces-0.10.6.win32-py3.4.exe
Size 214.6 kB
Tags Source
SHA-256 checksum
How to use checksums
563a18f942a9c9f64eed27fe2a1b3dfb5866a440cdaf4d833213798699cc1789
BLAKE2b-256 checksum
How to use checksums
89e415ea1e249f1f4b05a0e165992f1f106b1a9655d4528350e3c069bb4e8da5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / netifaces-0.10.6.win32-py2.7.exe

Download URL netifaces-0.10.6.win32-py2.7.exe
Size 218.7 kB
Tags Source
SHA-256 checksum
How to use checksums
337f0fae970ab7a9acf5690516f7c7795f41934350cc1e8ad33c5c0331904ac0
BLAKE2b-256 checksum
How to use checksums
c050eb24a9cbcfa2f13957457cbb159d8aa1fd14a67e0d78f987dd1d6de7b0e8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / netifaces-0.10.6-py3.5-win-amd64.egg

Download URL netifaces-0.10.6-py3.5-win-amd64.egg
Size 16.5 kB
Tags Egg
SHA-256 checksum
How to use checksums
7925add91982cb689963cc28fb8718c006f7713b527d262e32b29b4491cec295
BLAKE2b-256 checksum
How to use checksums
c29f0b7af93da491ca851e3afa6eed8de3d51ca9c6d23e6e4bde6ff481a9a31e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / netifaces-0.10.6-py3.5-win32.egg

Download URL netifaces-0.10.6-py3.5-win32.egg
Size 15.3 kB
Tags Egg
SHA-256 checksum
How to use checksums
2245677ee3aa1244bbd0fbf3d6e0158d38b612eba406e7be9639e7efe0371bfa
BLAKE2b-256 checksum
How to use checksums
c3ca32955889b4ae10429c5ea143d548a456b681187affbe9ca1aafb3fdc5388
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / netifaces-0.10.6-py3.4-win-amd64.egg

Download URL netifaces-0.10.6-py3.4-win-amd64.egg
Size 15.6 kB
Tags Egg
SHA-256 checksum
How to use checksums
3b19bf224b3e46c62f5f5e65a9fbd2e9731cda09289c76aca110a3dbdf0c3332
BLAKE2b-256 checksum
How to use checksums
9d3907a40889db708a9457ed980e3507d18dec5111b95f56c3abcec0a92f9fe3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / netifaces-0.10.6-py3.4-win32.egg

Download URL netifaces-0.10.6-py3.4-win32.egg
Size 14.8 kB
Tags Egg
SHA-256 checksum
How to use checksums
5a0114933657eebe4985fdf7b0099a27ec75501901000770addca6ad7bd23008
BLAKE2b-256 checksum
How to use checksums
e20893fd8ae8c2ff7c50539911af5efdf6102f3f1abb3b147f8fc2a7b636c992
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / netifaces-0.10.6-py2.7-win-amd64.egg

Download URL netifaces-0.10.6-py2.7-win-amd64.egg
Size 13.9 kB
Tags Egg
SHA-256 checksum
How to use checksums
8c3a2c7d573511507f0f29c9d1a28ce1b2a958b8d0d7a1b1966c6fd0fa5d2953
BLAKE2b-256 checksum
How to use checksums
83b7c4cd9a6ef8342f1c724b206fd8c54d452d6dd9f8d566d118dc70f03c528a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / netifaces-0.10.6-py2.7-win32.egg

Download URL netifaces-0.10.6-py2.7-win32.egg
Size 13.7 kB
Tags Egg
SHA-256 checksum
How to use checksums
a0c7c19e1fb62ac6018582f72d15ac056e75c3d2ab222fb25369e7766ed67453
BLAKE2b-256 checksum
How to use checksums
0500c719457bcb8f14f9a7b9244c3c5e203c40d041a364cf784cf554aaef8129
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / netifaces-0.10.6-cp35-cp35m-win_amd64.whl

Download URL netifaces-0.10.6-cp35-cp35m-win_amd64.whl
Size 20.6 kB
Tags CPython 3.5 CPython 3.5 pymalloc Windows x86-64
SHA-256 checksum
How to use checksums
f8b352247ae4b6731192d33fd35b27f247e3e4618a2d5cf65de41d46bbb53223
BLAKE2b-256 checksum
How to use checksums
7cb26b280e094225fa77702fb78f945a6d0df5bab18fbb71f2cf3b6622af073c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / netifaces-0.10.6-cp35-cp35m-win32.whl

Download URL netifaces-0.10.6-cp35-cp35m-win32.whl
Size 19.3 kB
Tags CPython 3.5 CPython 3.5 pymalloc Windows x86-32
SHA-256 checksum
How to use checksums
48275e627ce9220acfed2e1ca1e4cf01f58940412f2aebac7995750b50232701
BLAKE2b-256 checksum
How to use checksums
13a9c45975ae5e180a9efa029111a94e604ed5067cd84ebc54498c441bd1ea0d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / netifaces-0.10.6-cp34-cp34m-win_amd64.whl

Download URL netifaces-0.10.6-cp34-cp34m-win_amd64.whl
Size 19.7 kB
Tags CPython 3.4 CPython 3.4 pymalloc Windows x86-64
SHA-256 checksum
How to use checksums
61fd2706de21aac11475c921ba0fd98af19d5280702a11c5c8e2e910765dc378
BLAKE2b-256 checksum
How to use checksums
52f3968c9e0ecd68d1dd1299ef8773cd84d76e11265c2bf6a54a4e8ed97041c2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / netifaces-0.10.6-cp34-cp34m-win32.whl

Download URL netifaces-0.10.6-cp34-cp34m-win32.whl
Size 18.8 kB
Tags CPython 3.4 CPython 3.4 pymalloc Windows x86-32
SHA-256 checksum
How to use checksums
4ddf0f329d83516bba096b7eb1ad2ee354a98e2483f89ad3a590e653ece963c8
BLAKE2b-256 checksum
How to use checksums
181f59176c085cb100212c2e95567509037683aec9e2d79915a2688994af0985
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / netifaces-0.10.6-cp27-cp27m-win_amd64.whl

Download URL netifaces-0.10.6-cp27-cp27m-win_amd64.whl
Size 18.1 kB
Tags CPython 2.7 CPython 2.7 pymalloc Windows x86-64
SHA-256 checksum
How to use checksums
ef223d45b73cc96c25a6295f471106b3195d2367b7f153e43490673d89e9240e
BLAKE2b-256 checksum
How to use checksums
4c9c386ea453d5c8d14b9648f8f4ee9cc2e5f06796a1b321135595e31c2c68d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / netifaces-0.10.6-cp27-cp27m-win32.whl

Download URL netifaces-0.10.6-cp27-cp27m-win32.whl
Size 17.8 kB
Tags CPython 2.7 CPython 2.7 pymalloc Windows x86-32
SHA-256 checksum
How to use checksums
137a77c2e0a68a3e409a532fe73340c3df6a59ffe8eb565ec8b1f0a131402d09
BLAKE2b-256 checksum
How to use checksums
383af51286e6789ed1838389da651ab94c22f7a01ae00a6cd56506114f6c964f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.10.6 This release

19 release files

0.10.5

5 release files

0.8

1 release file

0.7

0.6

0.5

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page