0

Any ideas on how to make it so that I dont get Error 429: Too Many Request

Just a beginner here, I am trying to get ipData from my database which consists of Office365 Audit data..

Am i using the wrong library for such task?

def ipInfo(addr=''):
    from urllib.request import urlopen
    from json import load
    if addr == '':
        url = 'https://ipinfo.io/json'
    else:
        url = 'https://ipinfo.io/' + addr + '/json'
    res = urlopen(url)
    JSONtext = ''
    #response from url(if res==None then check connection)
    data = load(res)
    #will load the json response into data
    for attr in data.keys():
        #will print the data line by line
        if attr == 'ip':
            JSONtext = JSONtext + '{' + '"' + ''.join(attr) + '"' + ':' + '"' + ''.join(data[attr]) + '"'
        elif attr == 'readme':
            JSONtext = JSONtext + '"'+ ''.join(attr) + '"'+':'+'"' + ''.join(data[attr]) + '"' + '}'
        else:
            JSONtext = JSONtext  + '"'+ ''.join(attr) +'"'+':'+'"'+  ''.join(data[attr])  + '"'
    return JSONtext
#get table list
crsr = connection().cursor()
crsr.execute(
    "SELECT id,creationdate, userids, operations,auditdata ->> 'ClientIP' AS client_ip,ipdata FROM audits  WHERE operations ILIKE '%login%' LIMIT 5;")
tpl = crsr.fetchall()
crsr.close()

#append list to dictionary
dict = {"id":[],"creationdate":[],"userids":[],"operations":[],"clientip":[],"ipdata":[]}

for items in tpl:
    #converts item into list
    datalist = list(items)
    print ('Processing')

    for i in datalist:
        if i  == datalist[0]:
            dict["id"].append(i)
        elif i == datalist[1]:
            dict["creationdate"].append(i)
        elif i == datalist[2]:
            dict["userids"].append(i)
        elif i == datalist[3]:
            dict["operations"].append(i)
        elif i == datalist[4]:
            dict["clientip"].append(i)
            ip = i
            ip = ''.join(ip)
        else:
            dict["ipdata"].append(ipInfo(ip))
            time.sleep(6)

print('Task Completed')
df = pd.DataFrame.from_dict(dict)
Tech Marc
  • 3
  • 1
  • You are calling a remote web service with rate limit. Why don't you host the geolocation locally. In this case, there is no more concern about the query limit. – Michael C. Sep 02 '21 at 02:08
  • Alternatively, you could limit your requests to e.g. one per second. The error response from the server might have additional information in the form of x-headers telling you to wait a certain amount of time before sending another request. – Jan Wilamowski Sep 02 '21 at 02:12
  • Does this answer your question? [the server responded with a status of 429 (Too Many Requests) intlTelInput.js](https://stackoverflow.com/questions/31704941/the-server-responded-with-a-status-of-429-too-many-requests-intltelinput-js) – Maurice Meyer Sep 02 '21 at 10:50

0 Answers0