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.4.tar.gz
cd netifaces-0.10.4
python setup.py install

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-2014 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.4.tar.gz (23.0 kB view details)

Uploaded Source

Built Distributions

netifaces-0.10.4.win32-py3.4.exe (212.8 kB view details)

Uploaded Source

netifaces-0.10.4.win32-py3.3.exe (212.8 kB view details)

Uploaded Source

netifaces-0.10.4.win32-py2.7.exe (218.0 kB view details)

Uploaded Source

netifaces-0.10.4.win32-py2.6.exe (218.2 kB view details)

Uploaded Source

netifaces-0.10.4-py3.4-win32.egg (13.1 kB view details)

Uploaded Source

netifaces-0.10.4-py3.3-win32.egg (13.2 kB view details)

Uploaded Source

netifaces-0.10.4-py2.7-win32.egg (13.1 kB view details)

Uploaded Source

netifaces-0.10.4-py2.6-win32.egg (13.4 kB view details)

Uploaded Source

netifaces-0.10.4-cp34-none-win_amd64.whl (17.3 kB view details)

Uploaded CPython 3.4 Windows x86-64

netifaces-0.10.4-cp34-none-win32.whl (17.1 kB view details)

Uploaded CPython 3.4 Windows x86

netifaces-0.10.4-cp33-none-win_amd64.whl (17.3 kB view details)

Uploaded CPython 3.3 Windows x86-64

netifaces-0.10.4-cp33-none-win32.whl (17.1 kB view details)

Uploaded CPython 3.3 Windows x86

netifaces-0.10.4-cp27-none-win_amd64.whl (17.4 kB view details)

Uploaded CPython 2.7 Windows x86-64

netifaces-0.10.4-cp27-none-win32.whl (17.2 kB view details)

Uploaded CPython 2.7 Windows x86

netifaces-0.10.4-cp26-none-win_amd64.whl (17.4 kB view details)

Uploaded CPython 2.6 Windows x86-64

netifaces-0.10.4-cp26-none-win32.whl (17.5 kB view details)

Uploaded CPython 2.6 Windows x86

File details

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

File metadata

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

File hashes

Hashes for netifaces-0.10.4.tar.gz
Algorithm Hash digest
SHA256 9656a169cb83da34d732b0eb72b39373d48774aee009a3d1272b7ea2ce109cde
MD5 36da76e2cfadd24cc7510c2c0012eb1e
BLAKE2b-256 18fadd13d4910aea339c0bb87d2b3838d8fd923c11869b1f6e741dbd0ff3bc00

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for netifaces-0.10.4.win32-py3.4.exe
Algorithm Hash digest
SHA256 7598afb41e9cb8091b3fe9a1bd3e3a14eb8d2ef2f8bfca50d799ec7cab31fd65
MD5 efb919134a5e9059134fc449b1e7a631
BLAKE2b-256 4a11bac52c9f693d759ffbaa04dce814b441f3518bdc4bd9c2e2b55812fdd9ff

See more details on using hashes here.

File details

Details for the file netifaces-0.10.4.win32-py3.3.exe.

File metadata

File hashes

Hashes for netifaces-0.10.4.win32-py3.3.exe
Algorithm Hash digest
SHA256 72f752885fa44b97c727dca0597556e98cac1761db5913b0a8e915668c50d67e
MD5 58b03cb0fef45382a7085855aeb3cad6
BLAKE2b-256 01ebfcd67c02b96910a3782abdd73e75d8cc83280498f776793aa26dd6d976bf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for netifaces-0.10.4.win32-py2.7.exe
Algorithm Hash digest
SHA256 5f82ad3a6eeed2999365eaf48bcc5101732db46932d6252daf9ee8b67af5bbda
MD5 5cddb5bb1938948585d15f90f5e22b9d
BLAKE2b-256 573c711632ade88b6e2650cdbd8c8010f16abf6d6ebd887bd0d93385dc28bd38

See more details on using hashes here.

File details

Details for the file netifaces-0.10.4.win32-py2.6.exe.

File metadata

File hashes

Hashes for netifaces-0.10.4.win32-py2.6.exe
Algorithm Hash digest
SHA256 0596587cdbb4658d25b549044caae843d8605acedbbc1f5ca13f5245416ae436
MD5 4b92944c24aa25567a056245df0ced5d
BLAKE2b-256 50148d4f5c4043be4b36f2f39811a709f15e9721f6b224c3b3849d5f46d9d1c4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for netifaces-0.10.4-py3.4-win32.egg
Algorithm Hash digest
SHA256 a153348be2e36f587124fa82c292f440afe23747abf97599258fc04ced935276
MD5 9c60c5490a9edd58a723220a59aa8000
BLAKE2b-256 e7641aaeb88606ee02a31a69d18ff9363710016a49ead548c6cd195eda65da59

See more details on using hashes here.

File details

Details for the file netifaces-0.10.4-py3.3-win32.egg.

File metadata

File hashes

Hashes for netifaces-0.10.4-py3.3-win32.egg
Algorithm Hash digest
SHA256 e3c7bd946bd766c64c7ae2e7083873fa0254b9eb5f001fc590587e12f27717df
MD5 1d04bd53a60e4c7c8bdb857dc0716789
BLAKE2b-256 cbf4adc8d9b95c87d6d053c09fcf2d4525d432a0cb8ee189fa598fc51513e492

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for netifaces-0.10.4-py2.7-win32.egg
Algorithm Hash digest
SHA256 461c8b4fa0bb3bd9cd777b0d3393f4fc9d0a27b604486f6f372bb55e529f36f3
MD5 69c36401814f5f3cfd190ea203edc0be
BLAKE2b-256 196db4f294e30ec5cf723a8324e944d167a79aa966341de31a724498e7099fa6

See more details on using hashes here.

File details

Details for the file netifaces-0.10.4-py2.6-win32.egg.

File metadata

File hashes

Hashes for netifaces-0.10.4-py2.6-win32.egg
Algorithm Hash digest
SHA256 feb5d30b1ce5eeb53f92a607506a3f18e6de5029cec2b08d7c4b3f99910906d3
MD5 9ae37cabc4574c874a3053678b857aa1
BLAKE2b-256 c409c544837c85141e26c57d381169a37ce91014f08813bb919aa56e12cc21d7

See more details on using hashes here.

File details

Details for the file netifaces-0.10.4-cp34-none-win_amd64.whl.

File metadata

File hashes

Hashes for netifaces-0.10.4-cp34-none-win_amd64.whl
Algorithm Hash digest
SHA256 385c045b4e621eb4234fcacb77c9b1e1aecfdf277873e6263d98c74991d483b4
MD5 f8a88f70738fc774ed54b24d06d63805
BLAKE2b-256 942e4b1d68210216c3d404e89f0c332382b817b7dca037ac7d1e24f520cba204

See more details on using hashes here.

File details

Details for the file netifaces-0.10.4-cp34-none-win32.whl.

File metadata

File hashes

Hashes for netifaces-0.10.4-cp34-none-win32.whl
Algorithm Hash digest
SHA256 9c4b35f71b1d730781ee0e17407193d2987afe5ee4f94aea2615bf657cd4e012
MD5 e5da587a36cd07326b9fcadb73b660b9
BLAKE2b-256 eaf635b344ec3513f30e2e93f4fceaffb0d589027eba7d9e96a6df6f9c08207f

See more details on using hashes here.

File details

Details for the file netifaces-0.10.4-cp33-none-win_amd64.whl.

File metadata

File hashes

Hashes for netifaces-0.10.4-cp33-none-win_amd64.whl
Algorithm Hash digest
SHA256 e23fcfc3a2331405ab0c43096e75e177ed54a9c2782ec95eb7636379a5284366
MD5 a627c4923b53a5bcc408130d99aeaf1d
BLAKE2b-256 4fa42fca4d91a99e5f9fd436d52bb75b2cd76b97c93668d0f42902c952e05075

See more details on using hashes here.

File details

Details for the file netifaces-0.10.4-cp33-none-win32.whl.

File metadata

File hashes

Hashes for netifaces-0.10.4-cp33-none-win32.whl
Algorithm Hash digest
SHA256 f1a716ed6c3b8dfc2718107ae5d3e3eadb2956e8697a65598028edeeb30b28fa
MD5 f4a0d94330bd9368643dfbadd2cfeccd
BLAKE2b-256 d5d8b4e95f15c746dbdd088cb6ff8f9b00614cea7dd4a284b9f4455d0ddcd319

See more details on using hashes here.

File details

Details for the file netifaces-0.10.4-cp27-none-win_amd64.whl.

File metadata

File hashes

Hashes for netifaces-0.10.4-cp27-none-win_amd64.whl
Algorithm Hash digest
SHA256 c6a94c5c78ed0c1b00b5c62736485723135b3ebf5c86743aca372a7ac80663e4
MD5 fa59bf552090a106a0818c4390abbc77
BLAKE2b-256 f2e3f9fb99d4049ed7122ce8f9c9923ed52b5f1fb4a38540e5aaa5b0f452ba5d

See more details on using hashes here.

File details

Details for the file netifaces-0.10.4-cp27-none-win32.whl.

File metadata

File hashes

Hashes for netifaces-0.10.4-cp27-none-win32.whl
Algorithm Hash digest
SHA256 6e860b69abc9dd71353daddadc142b7628517737c092f7b3dd9ff26f446e8048
MD5 ac3b5700ce8fe0448758cd32e6a23df9
BLAKE2b-256 f729afcffc8fd714ae2bf45ef9f2a2d804d1723b2a7240b77a01280257e18f4e

See more details on using hashes here.

File details

Details for the file netifaces-0.10.4-cp26-none-win_amd64.whl.

File metadata

File hashes

Hashes for netifaces-0.10.4-cp26-none-win_amd64.whl
Algorithm Hash digest
SHA256 99b0de2cf9644394d75d84b92e9a4961d76b30b33961cd1ed80faeb09be7195f
MD5 f567414520b40fb7e931b5efb6a27b5a
BLAKE2b-256 eaebf68ddd71593f9b10adbcd6c3e59f1eae058d5c9d6ef52256caf4830bdf71

See more details on using hashes here.

File details

Details for the file netifaces-0.10.4-cp26-none-win32.whl.

File metadata

File hashes

Hashes for netifaces-0.10.4-cp26-none-win32.whl
Algorithm Hash digest
SHA256 112ee5b5a38e731ba39f3203a62796ebb7e234e976cba76b664535096078a6a2
MD5 c1e5b4b7e0525893402ccd5192187601
BLAKE2b-256 bc7ca4b071ce6fe8ae228414d8be62f80e2a8f8c9203b6a6b0c62b2eb16181a7

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