2

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"
Qwerty
  • 1,252
  • 1
  • 11
  • 23
  • I had the same issue only to realize that the website had certain IP restrictions, – ovicko Dec 30 '17 at 03:29
  • @ovicko it doesn't take a long time at all to load in my browser? – Qwerty Dec 30 '17 at 03:30
  • 3
    They could be discriminating on your User-Agent. – Jonathon Reinhart Dec 30 '17 at 03:32
  • how offen do you load it in script and in browser ? It may block if you do many requests in short time - ie. 1000 requests in 1 second. You should ask portal's admins for this problem. – furas Dec 30 '17 at 03:33
  • @ovicko I tried different URLs and added the chrome user agent to the headers and it still takes really long (like 20 seconds). – Qwerty Dec 30 '17 at 03:35
  • @furas It is slow with any URL – Qwerty Dec 30 '17 at 03:36
  • I tried in browser `http://blockexplorer.com/api/addr/19SokJG7fgk8iTjemJ2obfMj14FM16nqzj/balance` (I took this from examples) and it tooks many seconds and finally it shows error 502, so server has some internal problem. Do you have url which works fast in browser ? – furas Dec 30 '17 at 03:45
  • @furas this works for me at normal speed in browser: https://blockexplorer.com/api/addr/15u8aAPK4jJ5N8wpWJ5gutAyyeHtKX5i18/balance and remember, requests takes a long time no matter what URL I give it. – Qwerty Dec 30 '17 at 03:47
  • I opend it second time and it works correctly in browser in script - it seems server sometimes has problem with connections. – furas Dec 30 '17 at 03:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/162203/discussion-between-qwerty-and-furas). – Qwerty Dec 30 '17 at 03:48
  • I run it again in browser and again I see error 502 - so server has problem. When URL doesn't work in browser then it also has problem in script. When it works in browser then I don't have problem in script too. – furas Dec 30 '17 at 03:51
  • @furas I'm not worried about the server errors, I am more worried about the fact that it takes the same amount of time to load google or any other website. – Qwerty Dec 30 '17 at 03:53
  • if server has problems and gives error then you can't load this page fast. Do you have problem to load Google too ? Then you have totally different problem and it has nothing to do with your current question. – furas Dec 30 '17 at 03:57
  • BTW: it use icloud servers and it can run the same code on many servers in different places on the world, and people from different places may connect to different servers. Some may have problem, others not. – furas Dec 30 '17 at 04:01

1 Answers1

3

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.

hostingutilities.com
  • 8,894
  • 3
  • 41
  • 51
  • How is this an answer? This doesn't answer my question. Also interestingly enough, when run on my vps instead of my development computer, the 20 second wait is gone. – Qwerty Dec 30 '17 at 03:59
  • I made similar program with `time` and run it 100 times - it gives me similar times 0.3 - 1.0 :) – furas Dec 30 '17 at 03:59
  • @Qwerty, what do you get back when you print out r.text? – hostingutilities.com Dec 30 '17 at 04:02
  • @Mr.Me I get the balance as promised, but it still is taking at least 20 seconds to do the request – Qwerty Dec 30 '17 at 04:02
  • Does it take 20 seconds to do the request if you perform the request in your browser? – hostingutilities.com Dec 30 '17 at 04:07
  • @Mr.Me No, but I've cleared up the problem, you were right I believe they were ratelimiting me, and the only other URL I tried was Google, which needs certain headers to not ratelimit. This is the correct answer. – Qwerty Dec 30 '17 at 04:09
  • I'm glad you where able to clear up the problem. Cheers, and good luck with your cryptocurrency investments. – hostingutilities.com Dec 30 '17 at 04:14