您好我尝试请求一些json,一切正常,但有时请求出错,可能是网站堆栈或关闭连接对我来说,因为我每隔15分钟请求json。
代码在这里:
def request_coins(config):
coins = None
print(str(datetime.now()) + ' - REQUESTING Coins')
while coins is None:
try:
coins = (requests.get(url=str(config['UrlPath']['url']) + str(config['UrlPath']['userrates'])))
except:
print(str(datetime.now()) + " - Site didn't respond. Reconnecting in 10 sec")
time.sleep(10)
if coins is not None:
coins = coins.json()['coins']
print(str(datetime.now()) + ' - Coins received correctly')
return coins
问题是try-catch在我的情况下不起作用。在print(str(datetime.now()) + ' - REQUESTING Coins')
中请求堆栈,我没有收到任何异常和消息给cmd
print(str(datetime.now()) + " - Site didn't respond. Reconnecting in 10 sec")
我理解try:
coins = (requests.get(url=str(config['UrlPath']['url']) +
str(config['UrlPath']['userrates'])))
并且不要去except
:
知道如何解决?
答案 0 :(得分:0)
只需添加timeout
即可获得请求