IP version 6 or IPv6 is increasingly adopted by the industry to build newer applications. In case you would like to write an IPv6 application, the first thing you'd like to know is if your machine supports IPv6. This can be done from the Linux/Unix command line, as follows:
$ cat /proc/net/if_inet6 00000000000000000000000000000001 01 80 10 80 lo fe800000000000000a0027fffe950d1a 02 40 20 80 eth0
From your Python script, you can also check if the IPv6 support is present on your machine, and Python is installed with that support.
For this recipe, use pip
to install a Python third-party library, netifaces
, as follows:
$ pip install netifaces
We can use a third-party library, netifaces
, to find out if there is IPv6 support on your machine. We can call the interfaces()
function from this library to list all interfaces present in the system.
Listing 3.10 shows the Python IPv6 support checker, as follows:
#!/usr/bin/env python # Python Network Programming Cookbook -- Chapter – 3 # This program is optimized for Python 2.7. # It may run on any other version with/without modifications. # This program depends on Python module netifaces => 0.8 import socket import argparse import netifaces as ni def inspect_ipv6_support(): """ Find the ipv6 address""" print "IPV6 support built into Python: %s" %socket.has_ipv6 ipv6_addr = {} for interface in ni.interfaces(): all_addresses = ni.ifaddresses(interface) print "Interface %s:" %interface for family,addrs in all_addresses.iteritems(): fam_name = ni.address_families[family] print ' Address family: %s' % fam_name for addr in addrs: if fam_name == 'AF_INET6': ipv6_addr[interface] = addr['addr'] print ' Address : %s' % addr['addr'] nmask = addr.get('netmask', None) if nmask: print ' Netmask : %s' % nmask bcast = addr.get('broadcast', None) if bcast: print ' Broadcast: %s' % bcast if ipv6_addr: print "Found IPv6 address: %s" %ipv6_addr else: print "No IPv6 interface found!" if __name__ == '__main__': inspect_ipv6_support()
The output from this script will be as follows:
$ python 3_10_check_ipv6_support.py IPV6 support built into Python: True Interface lo: Address family: AF_PACKET Address : 00:00:00:00:00:00 Address family: AF_INET Address : 127.0.0.1 Netmask : 255.0.0.0 Address family: AF_INET6 Address : ::1 Netmask : ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff Interface eth0: Address family: AF_PACKET Address : 08:00:27:95:0d:1a Broadcast: ff:ff:ff:ff:ff:ff Address family: AF_INET Address : 10.0.2.15 Netmask : 255.255.255.0 Broadcast: 10.0.2.255 Address family: AF_INET6 Address : fe80::a00:27ff:fe95:d1a Netmask : ffff:ffff:ffff:ffff:: Found IPv6 address: {'lo': '::1', 'eth0': 'fe80::a00:27ff:fe95:d1a'}
The following screenshot shows the interaction between the IPv6 client and server:
The IPv6 support checker function, inspect_ipv6_support()
, first checks if Python is built with IPv6 using socket.has_ipv6
. Next, we call the interfaces()
function from the netifaces
module. This gives us the list of all interfaces. If we call the ifaddresses()
method by passing a network interface to it, we can get all the IP addresses of this interface. We then extract various IP-related information, such as protocol family, address, netmask, and broadcast address. Then, the address of a network interface has been added to the IPv6_address
dictionary if its protocol family matches AF_INET6
.
18.224.59.192