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.0.tar.gz
cd netifaces-0.10.0
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.

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.1.tar.gz (22.9 kB view details)

Uploaded Source

Built Distributions

netifaces-0.10.1.win32-py3.4.exe (212.5 kB view details)

Uploaded Source

netifaces-0.10.1.win32-py3.3.exe (212.5 kB view details)

Uploaded Source

netifaces-0.10.1.win32-py2.7.exe (217.7 kB view details)

Uploaded Source

netifaces-0.10.1.win32-py2.6.exe (218.0 kB view details)

Uploaded Source

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

Uploaded Source

netifaces-0.10.1-py3.3-win32.egg (13.1 kB view details)

Uploaded Source

netifaces-0.10.1-py2.7-win32.egg (13.0 kB view details)

Uploaded Source

netifaces-0.10.1-py2.6-win32.egg (13.3 kB view details)

Uploaded Source

netifaces-0.10.1-cp34-none-win32.whl (17.0 kB view details)

Uploaded CPython 3.4 Windows x86

netifaces-0.10.1-cp33-none-win32.whl (17.0 kB view details)

Uploaded CPython 3.3 Windows x86

netifaces-0.10.1-cp27-none-win32.whl (17.0 kB view details)

Uploaded CPython 2.7 Windows x86

netifaces-0.10.1-cp26-none-win32.whl (17.3 kB view details)

Uploaded CPython 2.6 Windows x86

File details

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

File metadata

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

File hashes

Hashes for netifaces-0.10.1.tar.gz
Algorithm Hash digest
SHA256 106d5e48dc92b4425093773ef13d730f87280ab00ad61f583c27657853fae8e3
MD5 4f92ca84e8cb1ce547998e7212ac313d
BLAKE2b-256 2d9ba14909a26c7cacddb29fd45ecfcc11cc6310a4d29b248700a31e052ab18e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for netifaces-0.10.1.win32-py3.4.exe
Algorithm Hash digest
SHA256 c682a2c3b3e2fe650dd9157e4751add17f5581dd0a63ea1e4194957558fc1c78
MD5 c191ecd57dd0d5d479a7d5b3607bd889
BLAKE2b-256 4633d74429c7b27bb6d1dd47308841cf87102e65634432eee662f2f7d9daa16f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for netifaces-0.10.1.win32-py3.3.exe
Algorithm Hash digest
SHA256 0caf8c23b33efa55d28c0d0e4a8918433b28537508b3bbe8b9ab7839f38e51ce
MD5 11b6f9f84eb42f53d574dfd9f1326164
BLAKE2b-256 f9c88532e1e3342d7f616ba2d75be7faaf8e844725b62bdaf22ed225e5bd0e8e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for netifaces-0.10.1.win32-py2.7.exe
Algorithm Hash digest
SHA256 76819bfd524994cefcb740c96c926d75a1bb0db42c95239fed6abc0c853713b2
MD5 b1584b3a9db0c763c94511f4e508f4fe
BLAKE2b-256 4c85f2b26d05d187e3b662636cf89e217338d00eb07da8d842fcc998e584ff4c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for netifaces-0.10.1.win32-py2.6.exe
Algorithm Hash digest
SHA256 a5fefce1aaaf171f86fb10265d5a5c0cba5531f838d5d8795ea945be8e36c645
MD5 bf229517b6b3d40ad0991fc6b504043d
BLAKE2b-256 8ec7b9fde96e351307fe56dda0a4fbbbd73ea99b1e7619708d0de3785e9db386

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for netifaces-0.10.1-py3.4-win32.egg
Algorithm Hash digest
SHA256 086c4541b434e083a805394bc074b5ab86179d8ea51c9ac0bba3e404422f2914
MD5 7ccc760d873a547d967876e180094f31
BLAKE2b-256 6db47be8cff01a51faf6be8180f1f639ce53939db0ffd0cb2b542c1b1aa926e2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for netifaces-0.10.1-py3.3-win32.egg
Algorithm Hash digest
SHA256 29fc6b14237baae429c94959fda57f0de9f23728ebaea89ee479baa5f5989dcb
MD5 f10939775f4ec4806e0d43582f090925
BLAKE2b-256 257155e01ee3327211626d3c8de575684760c81e5d5f1bcfeb24f3b8be4dcdca

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for netifaces-0.10.1-py2.7-win32.egg
Algorithm Hash digest
SHA256 20b25a5905bb6954e9980390e44719428f5b45ca8b8bf2b7d9440dcba1e11586
MD5 0295b17dc388d2cb5ac73a50dbd2e6e4
BLAKE2b-256 79e0cb9f38c1f27bc8b3921d1d7b0c50b5a542d810e1f7379b2b3cca8bd33298

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for netifaces-0.10.1-py2.6-win32.egg
Algorithm Hash digest
SHA256 2f63cbb298c6d34a1a15b0fdc3bc7fef0390466195d69877728aff9424814c88
MD5 c8c5f7e4a403dd416b435ad8eddefed1
BLAKE2b-256 57c541f096ae9cb5007bdbed7e91a19ac505ce656239c6577ecdda4d323f7d2e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for netifaces-0.10.1-cp34-none-win32.whl
Algorithm Hash digest
SHA256 dfaefacce28049c57dcb6410c694b61c68e2a4919e193c0d70411d4b095eeb74
MD5 0affb7fa40d190e1d250e0b5ef4ba032
BLAKE2b-256 e4a996ade6cea5071ada0f42597dac5ff97f541c96f4ff4173e6144068a918d9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for netifaces-0.10.1-cp33-none-win32.whl
Algorithm Hash digest
SHA256 de8419c83f695b66772dd880ee1bd598676bd0bd79ca3ab09ac69b182bbcdc66
MD5 bc6f84a67ece48d829f51e95afbf6fb7
BLAKE2b-256 05bece7b7b89fd69eeea5c14f5cba58f3335d933b5ab1cee09e9b396a7455b42

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for netifaces-0.10.1-cp27-none-win32.whl
Algorithm Hash digest
SHA256 470b47030e29962aef6a92834108508b304d395e055871fa56044577e5a90e6d
MD5 b42a29695f575f2893be1daed2b2dca3
BLAKE2b-256 6e2069a7a3cb27ccd82d87b4b09885fc8fdf586ccacb208d7af2bedcb7617b9b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for netifaces-0.10.1-cp26-none-win32.whl
Algorithm Hash digest
SHA256 ba1e477cede986e9aa03755923fd07ce608d3b2c3bc78540605c68bf582f405f
MD5 7965d5a882eaebf74ffcd043e697dc2f
BLAKE2b-256 b8fea373c465f00015a756b43fd6eb8defec6b1187e0ca941f2cb7e7e44cc005

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