#!/usr/bin/python
import ipaddr

def main():

  # Create some IPv4 addresses and networks, and some IPv6 addresses and networks as strings
  foo4 = '62.40.96.16'
  foonet4 = '62.40.96.0/24'
  bar4 = '62.40.101.145'
  barnet4 = '62.40.101.128/25'
  foo6 = '2001:798::4'
  foonet6 = '2001:798::/32'
  bar6 = '2001:798::422:66'
  barnet6 = '2001:798::/32'
  
  # Convert to ipaddr library objects
  foo4 = ipaddr.IPAddress(foo4)
  foonet4 = ipaddr.IPNetwork(foonet4)
  bar4 = ipaddr.IPAddress(bar4)
  barnet4 = ipaddr.IPNetwork(barnet4)
  foo6 = ipaddr.IPAddress(foo6)
  foonet6 = ipaddr.IPNetwork(foonet6)
  bar6 = ipaddr.IPAddress(bar6)
  barnet6 = ipaddr.IPNetwork(barnet6)

  # Obtain some useful information

  print("Let's test some IP addresses and IP networks:")

  if foo4 in foonet4:
    print("IPv" + str(foo4.version) + " address " +  str(foo4) + " is in network " + str(foonet4))

  print("IPv" + str(foonet4.version) + " network " +  str(foonet4) + " has broadcast address " + str(foonet4.broadcast))

  if foo4 not in barnet4:
    print("IPv" + str(foo4.version) + " address " +  str(foo4) + " is not in network " + str(barnet4))

  if foo6 in foonet6:
    print("IPv" + str(foo6.version) + " address " +  str(foo6) + " is in network " + str(foonet6))

  if bar4 in barnet4:
    print("IPv" + str(bar4.version) + " address " +  str(bar4) + " is in network " + str(barnet4))

  print("IPv" + str(barnet4.version) + " network " +  str(barnet4) + " has broadcast address " + str(barnet4.broadcast))

  if bar6 in barnet6:
    print("IPv" + str(bar6.version) + " address " +  str(bar6) + " is in network " + str(barnet6))

  exit(0)

if __name__ == "__main__":
  main()
