Python networking using the netaddr module
godarda@gd:~$ su
Password:
root@gd:/home/godarda# apt install python3-netaddr
...
Setting up python3-netaddr (0.8.0-2) ...
Processing triggers for man-db (2.11.2-1) ...
root@gd:/home/godarda# python3
...
>>> from netaddr import *
>>> ip=IPAddress('192.168.122.1')
>>> ip.version
4
>>> ip
IPAddress('192.168.122.1')
Numerical Representation
>>> int(ip) 3232266753 >>> hex(ip) '0xc0a87a01' >>> ip.bin '0b11000000101010000111101000000001' >>> ip.bits() '11000000.10101000.01111010.00000001' >>> ip.words (192, 168, 122, 1)
IPAddress Function
>>> ip=IPAddress('192.168.122.1').ipv6()
>>> ip
IPAddress('::ffff:192.168.122.1')
>>> ip.version
6
>>> ip.is_ipv4_mapped()
True
>>> ip.bin
'0b111111111111111111000000101010000111101000000001'
>>> ip.words
(0, 0, 0, 0, 0, 65535, 49320, 31233)
>>> int(ip)
281473914010113
>>> hex(ip)
'0xffffc0a87a01'
>>> ip.bits()
'0000000000000000:0000000000000000:0000000000000000:0000000000000000
:0000000000000000:1111111111111111:1100000010101000:0111101000000001'
>>> ip=IPAddress('::ffff:192.168.122.1').ipv4()
>>> ip
IPAddress('192.168.122.1')
>>> ip.is_multicast()
False
>>> ip.is_unicast()
True
>>> ip.is_private()
True
>>> ip.is_reserved()
False
>>> ip.is_netmask()
False
>>> ip.is_hostmask()
False
>>> IPAddress('255.255.255.255').is_netmask()
True
>>> IPAddress('255.255.255.255').is_hostmask()
True
>>> IPAddress('255.255.255.255').is_reserved()
True
>>> IPAddress('224.0.0.0').is_multicast()
True
>>> IPAddress('127.1.1.0').is_loopback()
True
>>> IPAddress('::1').is_loopback()
True
>>> IPAddress('::1')==IPAddress('127.1.1.0')
False
>>> ip.reverse_dns
'1.122.168.192.in-addr.arpa.'
IPNetwork Function
>>> ip=IPNetwork('192.168.122.1/24')
>>> ip
IPNetwork('192.168.122.1/24')
>>> ip.network
IPAddress('192.168.122.0')
>>> ip.broadcast
IPAddress('192.168.122.255')
>>> ip.netmask
IPAddress('255.255.255.0')
>>> ip.hostmask
IPAddress('0.0.0.255')
>>> ip.size
256
>>> ip.value=0
>>> ip.prefixlen=20
>>> ip
IPNetwork('0.0.0.0/20')
>>> ip.size
4096
>>> ip=IPNetwork('192.168.122.1/24')
>>> ip
IPNetwork('192.168.122.1/24')
>>> ip.cidr
IPNetwork('192.168.122.0/24')
>>> ip.ip.bits()
'11000000.10101000.01111010.00000001'
>>> ip.network.bits()
'11000000.10101000.01111010.00000000'
>>> ip.netmask.bits()
'11111111.11111111.11111111.00000000'
>>> ip.broadcast.bits()
'11000000.10101000.01111010.11111111'
>>> ip_list=list(ip)
>>> ip_list
... ...
>>> len(ip)
256
>>> len(ip_list)
256
>>> ip[-1]
IPAddress('192.168.122.255')
>>> ip[0]
IPAddress('192.168.122.0')
>>> ip[255]
IPAddress('192.168.122.255')
>>> IPNetwork('192.168.122.1/24')==IPNetwork('192.168.122.255/24')
True
>>> IPNetwork('192.168.122.1/24')==IPNetwork('192.168.122.0/24')
True
>>> IPNetwork('192.168.122.1/24').cidr<IPNetwork('192.168.122.0/24').cidr
False
IPRange Function
>>> ip=IPRange('192.168.122.1','192.168.122.15')
>>> ip.cidrs()
[IPNetwork('192.168.122.1/32'), IPNetwork('192.168.122.2/31'),
IPNetwork('192.168.122.4/30'), IPNetwork('192.168.122.8/29')]
>>> IPRange('192.168.122.0','192.168.122.255')==IPNetwork('192.168.122.1/24')
True
>>> IPRange('192.168.122.1','192.168.122.255')==IPNetwork('192.168.122.1/24')
False
IPSet Function
>>> IPSet()
IPSet([])
>>> IPSet(['192.168.122.1/24'])
IPSet(['192.168.122.1/24'])
>>> for ip in IPSet(['192.168.122.1/24']):
... print(ip)
...
192.168.122.0
192.168.122.1
192.168.122.2
192.168.122.3
192.168.122.4
192.168.122.5
.
.
.
>>> S1=IPSet()
>>> S1.add('192.168.122.1/24')
>>> S1
IPSet(['192.168.122.1/24'])
>>> S1.remove('192.168.122.1/24')
>>> S1
IPSet([])
>>> S1.add(IPRange('192.168.122.0','192.168.122.255'))
>>> S1
IPSet(['192.168.122.0/24'])
>>> S1.remove(IPRange('192.168.122.6','192.168.122.255'))
>>> S1
IPSet(['192.168.122.0/30', '192.168.122.4/31'])
>>> S1=IPSet(['192.168.122.1','192.168.122.255'])
>>> S2=IPSet(['192.168.122.1/24','192.168.122.255/24'])
>>> S1
IPSet(['192.168.122.1/32', '192.168.122.255/32'])
>>> S2
IPSet(['192.168.122.0/24'])
>>> S1.issuperset(S2)
False
>>> S2.issuperset(S1)
True
>>> S1.issubset(S2)
True
>>> S2.issubset(S1)
False
>>> S1 | S2
IPSet(['192.168.122.0/24'])
>>> S1 & S2
IPSet(['192.168.122.1/32', '192.168.122.255/32'])
>>> S1 - S2
IPSet([])
>>> S1 ^ S2
IPSet(['192.168.122.0/32', '192.168.122.2/31', '192.168.122.4/30', '192.168.122.8/29',
'192.168.122.16/28', '192.168.122.32/27', '192.168.122.64/26', '192.168.122.128/26',
'192.168.122.192/27', '192.168.122.224/28', '192.168.122.240/29', '192.168.122.248/30',
'192.168.122.252/31', '192.168.122.254/32'])
>>> S1.isdisjoint(S2)
False
>>> S2.update(IPRange('192.168.123.1','192.168.123.255'))
>>> S2
IPSet(['192.168.122.0/24', '192.168.123.1/32', '192.168.123.2/31',
'192.168.123.4/30', '192.168.123.8/29', '192.168.123.16/28',
'192.168.123.32/27', '192.168.123.64/26', '192.168.123.128/25'])
>>> S2.clear()
>>> S2
IPSet([])
>>> S1.remove('192.168.122.1')
>>> S1
IPSet(['192.168.122.255/32'])
>>> S1.iscontiguous()
True
Comments and Reactions
What Next?
Python program for two way communication using socket programming
Python program for file sharing using socket programmingPython Database ConnectivityAdvertisement