【问题标题】:Keep attempting urlopen until a connection is established - Python 2继续尝试 urlopen 直到建立连接 - Python 2
【发布时间】:2017-07-13 14:54:24
【问题描述】:

我的程序冻结有问题,我认为这是由于没有连接到 Poloniex 服务器。如何在建立连接之前一直循环 urlopen 请求?

这是我所拥有的:

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 的任何过多数据请求限制。

【问题讨论】:

  • 我现在也遇到了同样的问题,如果我能想出什么我会在这里发布答案......

标签: python-2.7 try-catch urllib2 urlopen poloniex


【解决方案1】:

这似乎对我有用。我增加了等待时间,这样它就不会在 IP 被禁止之前轰炸网站。

    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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多