-1

After some research I was able to get data via RESTful API from a networking device:

if sys.version_info >= (2,7,9):
    import ssl
    conn = httplib.HTTPSConnection('192.168.158.136', 443, context=ssl._create_unverified_context())
else:
    conn = httplib.HTTPSConnection('192.168.158.136', 443)

headers = {"Authorization"  : "Basic %s" % base64.b64encode('user:pass'),
           "Content-Type"   : "application/json"}

url="https://192.168.158.136/wapi/v1.2/network?_return_fields%2b=extattrs"

conn.request('GET', url, headers=headers)
response = conn.getresponse()

print response.read()
conn.close()

This prints me a json formatted list of the objects I need:

[
    {
        "_ref": "network/ZG5zLm5ldHdvcmskMTAuMC4wLjAvMjQvMA:10.0.0.0/24/default", 
        "extattrs": {
            "Location": {
                "value": "NAU"
            }
        }, 
        "network": "10.0.0.0/24", 
        "network_view": "default"
    }, 
    {
        "_ref": "network/ZG5zLm5ldHdvcmskMTAuMS4xLjAvMjQvMA:10.1.1.0/24/default", 
        "extattrs": {
            "Location": {
                "value": "BTN"
            }
        }, 
        "network": "10.1.1.0/24", 
        "network_view": "default"
    }, 
    {
        "_ref": "network/ZG5zLm5ldHdvcmskMTAuMi4yLjAvMjQvMA:10.2.2.0/24/default", 
        "extattrs": {
            "Location": {
                "value": "TRT"
            }
        }, 
        "network": "10.2.2.0/24", 
        "network_view": "default"
    }, 
    {
        "_ref": "network/ZG5zLm5ldHdvcmskMTkyLjE2OC4wLjAvMTYvMA:192.168.0.0/16/default", 
        "extattrs": {
            "Location": {
                "value": "MCW"
            }
        }, 
        "network": "192.168.0.0/16", 
        "network_view": "default"
    }
]

Now I need to "somehow" format that as a list which just contains the "network" and the content of the extattrs "Location" (and only Location, there will be others to, but I need Location only) in the format of:

Network Location, i.e.

10.0.0.0/24 NAU
10.1.1.0/24 BTN
10.2.2.0/24 TRT
192.168.0.0/16 MCW

I'm struggling in getting this done, i.e. reformatting the JSON code I get back from the query.

Cœur
  • 37,241
  • 25
  • 195
  • 267
f0rd42
  • 1,429
  • 4
  • 19
  • 30
  • 1
    Use the json module to convert this to a python data-structure. You will get a list of dicts. Walk over the list printing out the value for key `network'. – Anthon Apr 30 '15 at 13:24
  • @ForceBru : Thanks for editing, but "admin:infoblox" was already a dummy password, nothing real – f0rd42 Apr 30 '15 at 13:48
  • @AndreDieball, I didn't touch it. There is [another edit](http://stackoverflow.com/revisions/29968834/2) that affected this password – ForceBru Apr 30 '15 at 16:03

2 Answers2

0

You just have to use the json module in order to parse the JSON array into a dict.

import json

# Your code here

net_loc = []
resp_d = json.loads(response.read())
for obj in resp_d:
    net_loc.append(obj["network"] + " " + obj["extattrs"]["Location"]["value"])

The net_loc list will contain what you want.

Altomre
  • 66
  • 3
0

It's risky to post here map approach as SO people seems not like map, but i will give a try:

if sys.version_info >= (2,7,9):
    import ssl
    conn = httplib.HTTPSConnection('192.168.158.136', 443, context=ssl._create_unverified_context())
else:
    conn = httplib.HTTPSConnection('192.168.158.136', 443)

import json
headers = {"Authorization"  : "Basic %s" % base64.b64encode('user:pass'),
           "Content-Type"   : "application/json"}

url="https://192.168.158.136/wapi/v1.2/network?_return_fields%2b=extattrs"

conn.request('GET', url, headers=headers)
response = conn.getresponse()

data = json.loads(response.read())
conn.close()

#variant 1. "map" approach
networks = map(lambda x: "%s %s" % (x["network"], x["extattrs"]["Location"]["value"]), data)

print("\n".join(networks))

#variant 2. "for" approach
networks = []
for item in data:
  networks.append("%s %s" % (item["network"], item["extattrs"]["Location"])["value"]))

print("\n".join(networks))
Reishin
  • 1,854
  • 18
  • 21