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.3.tar.gz
cd netifaces-0.10.3
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.3.tar.gz (23.0 kB view details)

Uploaded Source

Built Distributions

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

Uploaded Source

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

Uploaded Source

netifaces-0.10.3.win32-py2.7.exe (217.9 kB view details)

Uploaded Source

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

Uploaded Source

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

Uploaded Source

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

Uploaded Source

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

Uploaded Source

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

Uploaded Source

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

Uploaded CPython 3.4 Windows x86

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

Uploaded CPython 3.3 Windows x86

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

Uploaded CPython 2.7 Windows x86

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

Uploaded CPython 2.6 Windows x86

File details

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

File metadata

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

File hashes

Hashes for netifaces-0.10.3.tar.gz
Algorithm Hash digest
SHA256 aea93f6c7d0840eb3bd5c38f1950b3d130148b9894453d67b3445adc9779332d
MD5 b96913473e1dcc3c4a7c43bc15d10e26
BLAKE2b-256 7265f1cc2cde4b13cb53b4ae399dde12a1d5922c977d9fe10a66c8566607949c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for netifaces-0.10.3.win32-py3.4.exe
Algorithm Hash digest
SHA256 b372e2129ec091c47e1a3f63870d62629d2653961d79b0abaaf54378f362cd0d
MD5 d62ab19a1cc07bfafcfb1a9f7660231b
BLAKE2b-256 a86f53c7f93e66737588e018e9147b324b9f595149813b1db0d2f822cdd23c64

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for netifaces-0.10.3.win32-py3.3.exe
Algorithm Hash digest
SHA256 818bd8609afbdb03ee7db29a6661449230d94be4b67879e75218010cf2ab5240
MD5 68f654fe15512642a708e40f5dc0b5b0
BLAKE2b-256 acbc4a593c419f9f10c5c5d3b4128a3e76ac83508c2953a3245ab09475c0c1d9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for netifaces-0.10.3.win32-py2.7.exe
Algorithm Hash digest
SHA256 3d2dd408e9f5142414480ca9379230344555b881fbbb1220144a2adfc9ce7f46
MD5 78647c9bade6880b99c7462a46648bc5
BLAKE2b-256 f3d4f9ab21da4ffa4d21146919abaf9d833bdedc902c94555624af43c6bfddc2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for netifaces-0.10.3.win32-py2.6.exe
Algorithm Hash digest
SHA256 fed1b62fcb355715a5afa15d0fa09efd3e20731ae7fcbc8e5efe2aa1cb25ae35
MD5 da9e984985970d7ced4dd69314ac0e14
BLAKE2b-256 17b2a791b99ea95ab24e493a6256d40ca0ec2ccf02ae0038b2ccf4dbb8fc12e2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for netifaces-0.10.3-py3.4-win32.egg
Algorithm Hash digest
SHA256 62816b77add3a65e26d539b1961282b8fb91482cae86f5882f6d643f1171c743
MD5 dcc30c3564308115b86046bb45659ccb
BLAKE2b-256 afca839909bf3a9e5eebb8c59aa985132b7a4735de4acacdb134379281046188

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for netifaces-0.10.3-py3.3-win32.egg
Algorithm Hash digest
SHA256 c62865658d593a4400985adf9ba87f81fb53ce76a00d1cbe4bdd3c540dd020dd
MD5 95a1732bd403d7afb1ab02ca230cc40c
BLAKE2b-256 70e686a38b2259d1f9f133cf47a6fe12e4cdf36f4077afb6bab12b2f3a3a7957

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for netifaces-0.10.3-py2.7-win32.egg
Algorithm Hash digest
SHA256 1e19c625202703cce57f5fe996daa5dfbcdb6b1e3b659093917f21780ef866e3
MD5 aacaa28c603070e4347630d76e5a5df9
BLAKE2b-256 28c129d7033b59a32bbe54713e753a8d0d9fc6f74e1c735d2935341a4fa8644d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for netifaces-0.10.3-py2.6-win32.egg
Algorithm Hash digest
SHA256 fc4d76462f6fe903f7f9feaa132179ff7937583b849e85f41186d80635905ec6
MD5 1b0c3b5a76bd543d45ec4d6fa50f29f4
BLAKE2b-256 cccef485c3259e0742ee20c20049358591195235faaddebcd5ad317d39391076

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for netifaces-0.10.3-cp34-none-win32.whl
Algorithm Hash digest
SHA256 ac9c28fe91d8acf7d704c58935e550c2270b519ce9c8a09ca860f135d2eb470e
MD5 1507d8639c9731472fa5162a103e1fcf
BLAKE2b-256 9467a34806beebb7e5e1ee66fc6a0e47cda69b23e113512521f609469aa8ff50

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for netifaces-0.10.3-cp33-none-win32.whl
Algorithm Hash digest
SHA256 bff634aead90c26f56b4b302610ffa94bf45c727fa4668bc77a0faa6f812d85b
MD5 0e46cbfc3480903e95c3b4ae8da1130f
BLAKE2b-256 80b6a0e535d8625cdc3a05fc0df9d88f8af3df7e67d9d392665e9757847b6850

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for netifaces-0.10.3-cp27-none-win32.whl
Algorithm Hash digest
SHA256 516638d105692115b1043f5cab243c63bb7b0e34310b6a43980ab4cdd0b65ffc
MD5 131b62392622c64c2ae2c7ad0a2522f7
BLAKE2b-256 0885266d9a83c2b193c58eb9485a4af9fbbcd2b3d18993e93143d98016662b39

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for netifaces-0.10.3-cp26-none-win32.whl
Algorithm Hash digest
SHA256 d2304dc1d208346582e113ae54f39571b2d674f6e7c86078c35cabb09ef73162
MD5 1343657ab8cd29bafd643da20f3aefd5
BLAKE2b-256 fd5c0632147acd7f4fb08ca4a6365b2ed7bca30a1b20b935b7e211925560c0c0

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