Advertisement
Guest User

JSON Export of Public Attribute of IPV4Network Object

a guest
Nov 19th, 2015
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import ipaddress
  2. import json
  3.  
  4. ipnet = ipaddress.IPv4Network('192.168.0.0/24')
  5.  
  6. print(ipnet)
  7.  
  8. def ipAttr(obj,att):
  9.         # Returns true for all non method attributes which are public
  10.         return not str(att).startswith('_') and 'method' not in str(type(getattr(obj,str(att))))
  11.  
  12. def ipDict(obj):
  13.         return {att: str(getattr(obj,att)) for att in dir(obj) if ipAttr(obj,att)}
  14.  
  15. print(json.dumps(ipDict(ipnet),sort_keys=True, indent=4, separators=(',', ': ')))
  16.  
  17. # output
  18.  
  19. 192.168.0.0/24
  20. {
  21.     "broadcast_address": "192.168.0.255",
  22.     "compressed": "192.168.0.0/24",
  23.     "exploded": "192.168.0.0/24",
  24.     "hostmask": "0.0.0.255",
  25.     "is_global": "False",
  26.     "is_link_local": "False",
  27.     "is_loopback": "False",
  28.     "is_multicast": "False",
  29.     "is_private": "True",
  30.     "is_reserved": "False",
  31.     "is_unspecified": "False",
  32.     "max_prefixlen": "32",
  33.     "netmask": "255.255.255.0",
  34.     "network_address": "192.168.0.0",
  35.     "num_addresses": "256",
  36.     "prefixlen": "24",
  37.     "version": "4",
  38.     "with_hostmask": "192.168.0.0/0.0.0.255",
  39.     "with_netmask": "192.168.0.0/255.255.255.0",
  40.     "with_prefixlen": "192.168.0.0/24"
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement