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

浪子不回头ぞ 提交于 2021-01-28 02:41:29

问题


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"

回答1:


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.



来源:https://stackoverflow.com/questions/48030675/python-requests-lib-is-taking-way-longer-than-it-should-to-do-a-get-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!