Skip to main content

Portable network interface information.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

netifaces-0.10.6.tar.gz (25.2 kB view details)

Uploaded Source

Built Distributions

netifaces-0.10.6.win-amd64-py3.5.exe (615.2 kB view details)

Uploaded Source

netifaces-0.10.6.win-amd64-py3.4.exe (246.6 kB view details)

Uploaded Source

netifaces-0.10.6.win-amd64-py2.7.exe (246.5 kB view details)

Uploaded Source

netifaces-0.10.6.win32-py3.5.exe (484.4 kB view details)

Uploaded Source

netifaces-0.10.6.win32-py3.4.exe (214.6 kB view details)

Uploaded Source

netifaces-0.10.6.win32-py2.7.exe (218.7 kB view details)

Uploaded Source

netifaces-0.10.6-py3.5-win-amd64.egg (16.5 kB view details)

Uploaded Source

netifaces-0.10.6-py3.5-win32.egg (15.3 kB view details)

Uploaded Source

netifaces-0.10.6-py3.4-win-amd64.egg (15.6 kB view details)

Uploaded Source

netifaces-0.10.6-py3.4-win32.egg (14.8 kB view details)

Uploaded Source

netifaces-0.10.6-py2.7-win-amd64.egg (13.9 kB view details)

Uploaded Source

netifaces-0.10.6-py2.7-win32.egg (13.7 kB view details)

Uploaded Source

netifaces-0.10.6-cp35-cp35m-win_amd64.whl (20.6 kB view details)

Uploaded CPython 3.5m Windows x86-64

netifaces-0.10.6-cp35-cp35m-win32.whl (19.3 kB view details)

Uploaded CPython 3.5m Windows x86

netifaces-0.10.6-cp34-cp34m-win_amd64.whl (19.7 kB view details)

Uploaded CPython 3.4m Windows x86-64

netifaces-0.10.6-cp34-cp34m-win32.whl (18.8 kB view details)

Uploaded CPython 3.4m Windows x86

netifaces-0.10.6-cp27-cp27m-win_amd64.whl (18.1 kB view details)

Uploaded CPython 2.7m Windows x86-64

netifaces-0.10.6-cp27-cp27m-win32.whl (17.8 kB view details)

Uploaded CPython 2.7m Windows x86

File details

Details for the file netifaces-0.10.6.tar.gz.

File metadata

  • Download URL: netifaces-0.10.6.tar.gz
  • Upload date:
  • Size: 25.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for netifaces-0.10.6.tar.gz
Algorithm Hash digest
SHA256 0c4da523f36d36f1ef92ee183f2512f3ceb9a9d2a45f7d19cda5a42c6689ebe0
MD5 1d424cb5ef52907c5ab913011122a98b
BLAKE2b-256 7201ba076082628901bca750bf53b322a8ff10c1d757dc29196a8e6082711c9d

See more details on using hashes here.

File details

Details for the file netifaces-0.10.6.win-amd64-py3.5.exe.

File metadata

File hashes

Hashes for netifaces-0.10.6.win-amd64-py3.5.exe
Algorithm Hash digest
SHA256 369eb616a6c844987bd4df486bb5f591aa0d5552378c6831f56ed81cfc228cab
MD5 1cb83e6d48a238caf5774bbac1ef37d2
BLAKE2b-256 1fb1ef234e36bff301160be80130f0d609909573aa36707da8dcef30b3e14898

See more details on using hashes here.

File details

Details for the file netifaces-0.10.6.win-amd64-py3.4.exe.

File metadata

File hashes

Hashes for netifaces-0.10.6.win-amd64-py3.4.exe
Algorithm Hash digest
SHA256 c455ca29737bf9b298408fd78a48f8fc6ddaa1f50a6eb92d814a8874412c631b
MD5 9213270faada396b21f4f4390d061616
BLAKE2b-256 3fd88199028c646f31b80177d8e38da61384985a103a6ea69d2d6ad0691bc36b

See more details on using hashes here.

File details

Details for the file netifaces-0.10.6.win-amd64-py2.7.exe.

File metadata

File hashes

Hashes for netifaces-0.10.6.win-amd64-py2.7.exe
Algorithm Hash digest
SHA256 60f25e5b6d2a682a394c87a6d2bf4d38c8dd8999ee32b955af88ceccaef7fe93
MD5 311f32b32f22b4278d3fce5f503c6348
BLAKE2b-256 3a49e8f4973c623cf03e46fc10e6c0b48c93b5b8c231a25aa616d09b2d198ad4

See more details on using hashes here.

File details

Details for the file netifaces-0.10.6.win32-py3.5.exe.

File metadata

File hashes

Hashes for netifaces-0.10.6.win32-py3.5.exe
Algorithm Hash digest
SHA256 88d8fa4fcccaca07519141e95b42f52fb650bed2e8f5b29c44e22968b92b7097
MD5 fd886799eda7b0356f63f6339e967126
BLAKE2b-256 b8fbc696726248db75c16cdeddd87f8a555d3a96208e937ef7bbfc301f833359

See more details on using hashes here.

File details

Details for the file netifaces-0.10.6.win32-py3.4.exe.

File metadata

File hashes

Hashes for netifaces-0.10.6.win32-py3.4.exe
Algorithm Hash digest
SHA256 563a18f942a9c9f64eed27fe2a1b3dfb5866a440cdaf4d833213798699cc1789
MD5 cfb5ec13ee19b5355877fdb585bf722b
BLAKE2b-256 89e415ea1e249f1f4b05a0e165992f1f106b1a9655d4528350e3c069bb4e8da5

See more details on using hashes here.

File details

Details for the file netifaces-0.10.6.win32-py2.7.exe.

File metadata

File hashes

Hashes for netifaces-0.10.6.win32-py2.7.exe
Algorithm Hash digest
SHA256 337f0fae970ab7a9acf5690516f7c7795f41934350cc1e8ad33c5c0331904ac0
MD5 32bd167068fe9088b505325ed81c8365
BLAKE2b-256 c050eb24a9cbcfa2f13957457cbb159d8aa1fd14a67e0d78f987dd1d6de7b0e8

See more details on using hashes here.

File details

Details for the file netifaces-0.10.6-py3.5-win-amd64.egg.

File metadata

File hashes

Hashes for netifaces-0.10.6-py3.5-win-amd64.egg
Algorithm Hash digest
SHA256 7925add91982cb689963cc28fb8718c006f7713b527d262e32b29b4491cec295
MD5 99c9b03eb959cfa8327bf46b059a8809
BLAKE2b-256 c29f0b7af93da491ca851e3afa6eed8de3d51ca9c6d23e6e4bde6ff481a9a31e

See more details on using hashes here.

File details

Details for the file netifaces-0.10.6-py3.5-win32.egg.

File metadata

File hashes

Hashes for netifaces-0.10.6-py3.5-win32.egg
Algorithm Hash digest
SHA256 2245677ee3aa1244bbd0fbf3d6e0158d38b612eba406e7be9639e7efe0371bfa
MD5 f5105e11f2c6a4a5be77c273bb1dc0e4
BLAKE2b-256 c3ca32955889b4ae10429c5ea143d548a456b681187affbe9ca1aafb3fdc5388

See more details on using hashes here.

File details

Details for the file netifaces-0.10.6-py3.4-win-amd64.egg.

File metadata

File hashes

Hashes for netifaces-0.10.6-py3.4-win-amd64.egg
Algorithm Hash digest
SHA256 3b19bf224b3e46c62f5f5e65a9fbd2e9731cda09289c76aca110a3dbdf0c3332
MD5 89a6c6eba4e26fc6324e527b61f965dc
BLAKE2b-256 9d3907a40889db708a9457ed980e3507d18dec5111b95f56c3abcec0a92f9fe3

See more details on using hashes here.

File details

Details for the file netifaces-0.10.6-py3.4-win32.egg.

File metadata

File hashes

Hashes for netifaces-0.10.6-py3.4-win32.egg
Algorithm Hash digest
SHA256 5a0114933657eebe4985fdf7b0099a27ec75501901000770addca6ad7bd23008
MD5 dd747943ec35c7de3575d33c3f2d24e3
BLAKE2b-256 e20893fd8ae8c2ff7c50539911af5efdf6102f3f1abb3b147f8fc2a7b636c992

See more details on using hashes here.

File details

Details for the file netifaces-0.10.6-py2.7-win-amd64.egg.

File metadata

File hashes

Hashes for netifaces-0.10.6-py2.7-win-amd64.egg
Algorithm Hash digest
SHA256 8c3a2c7d573511507f0f29c9d1a28ce1b2a958b8d0d7a1b1966c6fd0fa5d2953
MD5 2fcba64cf3e121b71d433aca0a57c2c5
BLAKE2b-256 83b7c4cd9a6ef8342f1c724b206fd8c54d452d6dd9f8d566d118dc70f03c528a

See more details on using hashes here.

File details

Details for the file netifaces-0.10.6-py2.7-win32.egg.

File metadata

File hashes

Hashes for netifaces-0.10.6-py2.7-win32.egg
Algorithm Hash digest
SHA256 a0c7c19e1fb62ac6018582f72d15ac056e75c3d2ab222fb25369e7766ed67453
MD5 adcc1bfae02e1373a7539c16ba4aeed2
BLAKE2b-256 0500c719457bcb8f14f9a7b9244c3c5e203c40d041a364cf784cf554aaef8129

See more details on using hashes here.

File details

Details for the file netifaces-0.10.6-cp35-cp35m-win_amd64.whl.

File metadata

File hashes

Hashes for netifaces-0.10.6-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 f8b352247ae4b6731192d33fd35b27f247e3e4618a2d5cf65de41d46bbb53223
MD5 42d86677543d1071a848012268f693b5
BLAKE2b-256 7cb26b280e094225fa77702fb78f945a6d0df5bab18fbb71f2cf3b6622af073c

See more details on using hashes here.

File details

Details for the file netifaces-0.10.6-cp35-cp35m-win32.whl.

File metadata

File hashes

Hashes for netifaces-0.10.6-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 48275e627ce9220acfed2e1ca1e4cf01f58940412f2aebac7995750b50232701
MD5 3acc5700f4ecf5cb8f23e5f1ab66732d
BLAKE2b-256 13a9c45975ae5e180a9efa029111a94e604ed5067cd84ebc54498c441bd1ea0d

See more details on using hashes here.

File details

Details for the file netifaces-0.10.6-cp34-cp34m-win_amd64.whl.

File metadata

File hashes

Hashes for netifaces-0.10.6-cp34-cp34m-win_amd64.whl
Algorithm Hash digest
SHA256 61fd2706de21aac11475c921ba0fd98af19d5280702a11c5c8e2e910765dc378
MD5 459fcb9c9d9ab46e0f7aa7417c3caa89
BLAKE2b-256 52f3968c9e0ecd68d1dd1299ef8773cd84d76e11265c2bf6a54a4e8ed97041c2

See more details on using hashes here.

File details

Details for the file netifaces-0.10.6-cp34-cp34m-win32.whl.

File metadata

File hashes

Hashes for netifaces-0.10.6-cp34-cp34m-win32.whl
Algorithm Hash digest
SHA256 4ddf0f329d83516bba096b7eb1ad2ee354a98e2483f89ad3a590e653ece963c8
MD5 f7ef4f05453c933c2fd4cdd0953b3f11
BLAKE2b-256 181f59176c085cb100212c2e95567509037683aec9e2d79915a2688994af0985

See more details on using hashes here.

File details

Details for the file netifaces-0.10.6-cp27-cp27m-win_amd64.whl.

File metadata

File hashes

Hashes for netifaces-0.10.6-cp27-cp27m-win_amd64.whl
Algorithm Hash digest
SHA256 ef223d45b73cc96c25a6295f471106b3195d2367b7f153e43490673d89e9240e
MD5 783feb401d55b77bfaa01dda7d528ca1
BLAKE2b-256 4c9c386ea453d5c8d14b9648f8f4ee9cc2e5f06796a1b321135595e31c2c68d2

See more details on using hashes here.

File details

Details for the file netifaces-0.10.6-cp27-cp27m-win32.whl.

File metadata

File hashes

Hashes for netifaces-0.10.6-cp27-cp27m-win32.whl
Algorithm Hash digest
SHA256 137a77c2e0a68a3e409a532fe73340c3df6a59ffe8eb565ec8b1f0a131402d09
MD5 23480d42e6524c59b1a835dc9bbf5128
BLAKE2b-256 383af51286e6789ed1838389da651ab94c22f7a01ae00a6cd56506114f6c964f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page