Search code examples
pythonpython-3.xgetpython-requests

Python requests lib is taking way longer than it should to do a get request


So I have this code. Whenever I run the code, and it gets to line 3, it takes about 20 whole seconds to do the get request. There is no reason it should be taking this long, and it's consistently taking long every time. Any help?

def get_balance(addr):
try:
    r = requests.get("http://blockexplorer.com/api/addr/"+addr+"/balance")
    return int(r.text)/10000000
except:
    return "e"

Solution

  • It works for me most of the time.

    >>> def get_balance(addr):
    ...   try:
    ...       start = time.time()
    ...       r = requests.get("http://blockexplorer.com/api/addr/"+addr+"/balance")
    ...       end = time.time()
    ...       print(f"took {end - start} seconds")
    ...       print(r.text, "satoshis")
    ...       return int(r.text)/100000000
    ...   except:
    ...       return "e"
    ...
    >>>
    >>> get_balance("1HB5XMLmzFVj8ALj6mfBsbifRoD4miY36v")
    took 0.7754228115081787 seconds
    151881086 satoshis
    15.1881086
    

    But if I do this enough times in a row, I'll occasionally get the error "Bitcoin JSON-RPC: Work queue depth exceeded. Code:429"

    Print out r.text like I did, and that might show you an error message from Block Explorer. It might be that they have started rate-limiting you.