2017-07-13 20 views
1

我遇到了我的程序冻结问题,我认为这是由于没有连接到Poloniex服务器。如何连接urlopen请求直到连接建立?继续尝试urlopen,直到连接建立 - Python 2

以下是我有:

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 

,并在主:

jsn = None 

count = 0; 
for pair in pairs: 

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

我检查了计时和我似乎没有被打破,从Poloniex任何过多的数据请求限制。

+0

我现在有同样的问题,如果我能拿出任何东西我会在这里发表一个答案...... – jamzsabb

回答

0

这似乎是为我工作。我增加了等待时间,以便它不会在知识产权被禁止之前轰炸现场。

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 
相关问题