1

I'm having a problem with my program freezing, I think it is due to no connection to the Poloniex server. How do I keep looping the urlopen request until a connection is established?

Here is what I have:

elif(command == "returnOrderBook"):
    try:
        ret = urllib2.urlopen(urllib2.Request('https://poloniex.com/public?command=' + command + '&currencyPair=' + str(req['currencyPair'])))
        return json.loads(ret.read())
    except:
        print('no connection')
    else: return None   

And in the main:

jsn = None

count = 0;
for pair in pairs:

    while(jsn == None):
        jsn = p.returnMarketTradeHistory (pair)
        if(jsn == None):
            print('jsn failed')    
            sleep(0.3)

I have checked the timings and I don't seem to be breaking any excessive data request limits from Poloniex.

mattstack
  • 51
  • 1
  • 12

1 Answers1

0

This seems to be working for me. I increased the wait times so that it won't just bombard the site until the IP gets banned.

    wait = 60
    while True:
        try:
            html = urlopen('http://www.example.com')
            wait = 60
            break
        except:
            print('Failed to open page')
            time.sleep(random.sample(range(wait, wait * 2), 1)[0])
            wait = (wait + 300) * 2
            pass
jamzsabb
  • 1,125
  • 2
  • 18
  • 40