Example 1, Find Matching PoPs

Compare two AS numbers in PeeringDB for matching PoPs/locations:

$ python peering-db-example-1.py 51551 41695
Match: Telehouse London (Docklands North)
Match: Telehouse London (Docklands East)
#!/usr/bin/python

import sys
import urllib2
import json

local_asn   = sys.argv[1]
local_pops  = {}
target_asn  = sys.argv[2]
target_pops = {}


# Grab local net ID in peeringdb based on AS number,
# then grab the net object using the net ID
http_object  = urllib2.urlopen("https://peeringdb.com/api/net?asn=%s" % local_asn)
json_dict    = json.load(http_object)
local_net_id = json_dict["data"][0]["id"]
http_object  = urllib2.urlopen("https://peeringdb.com/api/net/%s" % local_net_id)
json_dict    = json.load(http_object)

# Iterate the net object and store the net facilities
for pop in json_dict["data"][0]["netfac_set"]:
    local_pops[pop["fac_id"]] = pop["name"]



# Rinse and repeat for the target AS
http_object   = urllib2.urlopen("https://peeringdb.com/api/net?asn=%s" % target_asn)
json_dict     = json.load(http_object)
target_net_id = json_dict["data"][0]["id"]
http_object   = urllib2.urlopen("https://peeringdb.com/api/net/%s" % target_net_id)
json_dict     = json.load(http_object)

for pop in json_dict["data"][0]["netfac_set"]:
    target_pops[pop["fac_id"]] = pop["name"]



# Create a set of the matches between PoP sites and display
for shared_pop in set(local_pops).intersection(target_pops):
    print ("Match: %s" % local_pops[shared_pop])