Skip to main content

Portable network interface information.

Project description

Linux/macOS

Build Status (Linux/Mac)

Windows

Build Status (Windows)

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.7.tar.gz
cd netifaces-0.10.7
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 Github to send a pull request.

4. What license is this under?

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

Copyright (c) 2007-2018 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.7.tar.gz (29.3 kB view details)

Uploaded Source

Built Distributions

netifaces-0.10.7-cp36-cp36m-win_amd64.whl (16.0 kB view details)

Uploaded CPython 3.6m Windows x86-64

netifaces-0.10.7-cp36-cp36m-win32.whl (14.8 kB view details)

Uploaded CPython 3.6m Windows x86

netifaces-0.10.7-cp36-cp36m-manylinux1_x86_64.whl (32.3 kB view details)

Uploaded CPython 3.6m

netifaces-0.10.7-cp36-cp36m-manylinux1_i686.whl (36.3 kB view details)

Uploaded CPython 3.6m

netifaces-0.10.7-cp36-cp36m-macosx_10_13_x86_64.whl (11.6 kB view details)

Uploaded CPython 3.6m macOS 10.13+ x86-64

netifaces-0.10.7-cp35-cp35m-win32.whl (14.8 kB view details)

Uploaded CPython 3.5m Windows x86

netifaces-0.10.7-cp35-cp35m-manylinux1_x86_64.whl (32.3 kB view details)

Uploaded CPython 3.5m

netifaces-0.10.7-cp35-cp35m-manylinux1_i686.whl (36.3 kB view details)

Uploaded CPython 3.5m

netifaces-0.10.7-cp34-cp34m-win32.whl (13.3 kB view details)

Uploaded CPython 3.4m Windows x86

netifaces-0.10.7-cp34-cp34m-manylinux1_x86_64.whl (32.1 kB view details)

Uploaded CPython 3.4m

netifaces-0.10.7-cp34-cp34m-manylinux1_i686.whl (36.2 kB view details)

Uploaded CPython 3.4m

netifaces-0.10.7-cp33-cp33m-manylinux1_i686.whl (35.0 kB view details)

Uploaded CPython 3.3m

netifaces-0.10.7-cp27-cp27mu-manylinux1_x86_64.whl (31.0 kB view details)

Uploaded CPython 2.7mu

netifaces-0.10.7-cp27-cp27mu-manylinux1_i686.whl (35.0 kB view details)

Uploaded CPython 2.7mu

netifaces-0.10.7-cp27-cp27m-win32.whl (13.3 kB view details)

Uploaded CPython 2.7m Windows x86

netifaces-0.10.7-cp27-cp27m-manylinux1_x86_64.whl (31.0 kB view details)

Uploaded CPython 2.7m

netifaces-0.10.7-cp27-cp27m-manylinux1_i686.whl (35.0 kB view details)

Uploaded CPython 2.7m

netifaces-0.10.7-cp27-cp27m-macosx_10_13_x86_64.whl (11.5 kB view details)

Uploaded CPython 2.7m macOS 10.13+ x86-64

File details

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

File metadata

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

File hashes

Hashes for netifaces-0.10.7.tar.gz
Algorithm Hash digest
SHA256 bd590fcb75421537d4149825e1e63cca225fd47dad861710c46bd1cb329d8cbd
MD5 e0cfd6c38b39f3ea3185fa7503a81765
BLAKE2b-256 81394e9a026265ba944ddf1fea176dbb29e0fe50c43717ba4fcf3646d099fe38

See more details on using hashes here.

File details

Details for the file netifaces-0.10.7-cp36-cp36m-win_amd64.whl.

File metadata

File hashes

Hashes for netifaces-0.10.7-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 179f2463469fe69c829c96c7b332c7fd3f01652311e36ae11e409e5b34eb9dad
MD5 d9b3598953246b93290197ebe6c73672
BLAKE2b-256 00ba227e378b815a075bbda86e516e31d55430abdaa32941b72144117f7a8999

See more details on using hashes here.

File details

Details for the file netifaces-0.10.7-cp36-cp36m-win32.whl.

File metadata

File hashes

Hashes for netifaces-0.10.7-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 8a69dc2743dcbb9b87fa3453820852f0feabc17b03d3841619e8e63f5d3902d5
MD5 c3aa9a92617b2a0028817cafa7e3a086
BLAKE2b-256 ec8c3c02241344b339d166ce7a8b403cee99fe423a3c200a0bc421be19bcb9af

See more details on using hashes here.

File details

Details for the file netifaces-0.10.7-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for netifaces-0.10.7-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 4bb6b02b7c485a595a9d75346df3a77fcaa12d2352437c49c2d73ed968572d72
MD5 8825938fba3fef5c1003084f6ae3eafb
BLAKE2b-256 999eca74e521d0d8dcfa07cbfc83ae36f9c74a57ad5c9269d65d1228c5369aff

See more details on using hashes here.

File details

Details for the file netifaces-0.10.7-cp36-cp36m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for netifaces-0.10.7-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 1a4082a52f521ceeaf3d0ff25c61a06d46444f3578f487935652ecc93becf538
MD5 c6351e9d14f55c5b1fb9310ea6f47327
BLAKE2b-256 d2c5b7cbba20a8bcfdfda6d358f073becb1f19d53b670cc48ec68a41e2e1f08f

See more details on using hashes here.

File details

Details for the file netifaces-0.10.7-cp36-cp36m-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for netifaces-0.10.7-cp36-cp36m-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 0083ff8d89c559d0da0811c4930cf36e4945da0f03749e0f108678098d7d1607
MD5 01062b0768ac0b535bf8f2660a0cb2ae
BLAKE2b-256 687a0221a0d15637d31b059edb312c4bbb4eefbe859d08d09c2e208d3d22aa46

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for netifaces-0.10.7-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 19df6feff2af7a9179e42afdd01d79616d85b7ff4401b55ffce2df29d512a017
MD5 179412527ae15e9cc36b03375950ba31
BLAKE2b-256 d92f2066234ea726c352b02a36cd81f1bca57848bfc882901cb3fc4cf005690f

See more details on using hashes here.

File details

Details for the file netifaces-0.10.7-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for netifaces-0.10.7-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 1edeea7d739b1d716d15214039386e999f2e374aaeac0703092132b4e55ba461
MD5 ad8648d7ba65644d5cda21e0f965451c
BLAKE2b-256 e9256df71623bc2867ead3323265d548aa9bd27bfa5fd22b2486185d68b81c2b

See more details on using hashes here.

File details

Details for the file netifaces-0.10.7-cp35-cp35m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for netifaces-0.10.7-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 e1037cfad0e99a23fb4829f40302f3696395358950ba9f0315363a0e1eb04af6
MD5 582848a2f6910fc3a47e658dc267372f
BLAKE2b-256 4608ee4ca5e18bc1b04f2797eb93ecfb765745a7f54d195d1609e3a05449c449

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for netifaces-0.10.7-cp34-cp34m-win32.whl
Algorithm Hash digest
SHA256 674498dad41dacd86ec82e9e1793f9d8716755085c3776f051a266b1634a0b60
MD5 a62be172f4e1bed0c1f459646852b1fe
BLAKE2b-256 e45982fa5cd900261915f8ad5a43cc42cfb874e5352f164934464e4083fedbca

See more details on using hashes here.

File details

Details for the file netifaces-0.10.7-cp34-cp34m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for netifaces-0.10.7-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e76d38d9cff51ecf9fd5b8d0adf63f7b8875e1ac8548ccb52264939e308b771e
MD5 73cad756ec46a7a6fc9d682013313bf4
BLAKE2b-256 f7259c64ca9a1c734c3fb86ecf075cc13227dc17a70b82616df235834f146339

See more details on using hashes here.

File details

Details for the file netifaces-0.10.7-cp34-cp34m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for netifaces-0.10.7-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 38969c101f1e61c2a53af6a7b635f63e81085ae87413f1f5551a4d7057f5f773
MD5 0019dc9f26fe9339cc4ec00914fb12b5
BLAKE2b-256 2796ba0ea1e679c58f18940a75a0b9be8abcd2e313610c9972eac1adc97d060a

See more details on using hashes here.

File details

Details for the file netifaces-0.10.7-cp33-cp33m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for netifaces-0.10.7-cp33-cp33m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 7ea8eb1e824f74c161396f0d6d76fa3943462ee9a4629c387c10399d2aee058c
MD5 3ee5bd380eb574cf4fac333910ac481a
BLAKE2b-256 9860ad66f99b3244fa4b40d2dc89a7f48cd3e6ab8413db01e9e16435c2d645be

See more details on using hashes here.

File details

Details for the file netifaces-0.10.7-cp27-cp27mu-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for netifaces-0.10.7-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 9cf8cb2de7524c34808e6111dfb9f89e3b7c568e6953b3e02b8397447a6d8303
MD5 e020d32a050e27ae26a222e2ebbbef73
BLAKE2b-256 70dd411bb4cda549136cf9ff5dbd4f33f898fd90b25ac73fd4ea42a3d3e8fd15

See more details on using hashes here.

File details

Details for the file netifaces-0.10.7-cp27-cp27mu-manylinux1_i686.whl.

File metadata

File hashes

Hashes for netifaces-0.10.7-cp27-cp27mu-manylinux1_i686.whl
Algorithm Hash digest
SHA256 e6d52aee254f9cf6192b54c156c67d54dcf451bec6781580844af892e4bf36bb
MD5 ac31753bed87f5d7e800d2f0cffa2b68
BLAKE2b-256 06ef22159355df12a50df41f77767dbd8f92998f2b82273d163aa892d5e6c6d5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for netifaces-0.10.7-cp27-cp27m-win32.whl
Algorithm Hash digest
SHA256 a77263e046636a761a2c3eeb0a56b5f8fa64f865efec91a9be008a46412b4ddd
MD5 2978a13528b43ba25103269691018d30
BLAKE2b-256 a50f585ecf75085cf9097dcc2d9056d5a8d9d6f097d656c79dcbeb8db21cc16c

See more details on using hashes here.

File details

Details for the file netifaces-0.10.7-cp27-cp27m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for netifaces-0.10.7-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 2acb23ca092cc53b2b1f374132bbef5dd843767f6b10d31024f958474a1dfe96
MD5 c9481ceb6d43472456f239b3653f4c43
BLAKE2b-256 f62e0f6ca9c9d8e91a4116befbdb043c81725805a449be01b3a5b76775661c96

See more details on using hashes here.

File details

Details for the file netifaces-0.10.7-cp27-cp27m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for netifaces-0.10.7-cp27-cp27m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 4817871b226082600b64578549b9932bb07c1a42e9311ddd7c9dad08ff1fb22f
MD5 30e4b374cf1ff431721117c37a493f3f
BLAKE2b-256 3892e8e7817d7e05a2ec818ab72738c8b01f87beacae9b37105d9fbd9ff444d4

See more details on using hashes here.

File details

Details for the file netifaces-0.10.7-cp27-cp27m-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for netifaces-0.10.7-cp27-cp27m-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 aea569ce1a5a75b010758097199f84d9a3a109a696473c635bcf82f8a43cc551
MD5 1c4d7b8cf4b07780c14f0bf58b81ad1e
BLAKE2b-256 33f552d1b3ac7390293bfc2ecc321112693605607920159660337fffe09fa8eb

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