For marketers
who love technology
Home » , , , » Example python function to query RIPE and obtain all prefixes of an autonomous system

Example python function to query RIPE and obtain all prefixes of an autonomous system

English: Python logo Deutsch: Python Logo
Here is a python function to query RIPE and obtain all prefixes of an autonomous system. It is a good (simple) example of python urllib to query an API returning JSON formatted data.


import json
from urllib.request import urlopen


def ripe_get_prefixes_per_asn(asn):
    """ Uses RIPE's API (https://stat.ripe.net/data/announced-prefixes/data.?)
    to list the prefixes associated to a given Autonomous System Number (ASN)
    
    e.g., e.g. https://stat.ripe.net/data/announced-prefixes/data.json?resource=AS3333&starttime=2011-12-12T12:00
    
    This API is documented on https://stat.ripe.net/docs/data_api    
    """
    url_base='https://stat.ripe.net/data/announced-prefixes/data.json?resource=AS'
    rep = urlopen(url_base+str(asn)+'&starttime='+'2011-12-12T12:00')
    data= str(rep.read().decode(encoding='UTF-8'))
    rep.close()       
    js_data= json.loads(data)         
    pref_list=[]
    for record in js_data['data']['prefixes']:
        pref_list.append(record['prefix'])                       
    return pref_list 
 
SHARE

About Gilles

0 comments :

Post a Comment