我使用web服务来检索一些数据,但有时网址不起作用,我的网站没有加载.你知道我如何处理以下异常,所以如果webservice不工作,网站没有问题吗?
Django Version: 1.3.1 Exception Type: ConnectionError Exception Value: HTTPConnectionPool(host='test.com', port=8580): Max retries exceeded with url:
我用了
try: r = requests.get("http://test.com", timeout=0.001) except requests.exceptions.RequestException as e: # This is the correct syntax print e sys.exit(1)
但没有任何反应
您不应该退出您的工作器实例sys.exit(1)
此外您正在捕获错误的错误.
你可以做的例如是:
from requests.exceptions import ConnectionError try: r = requests.get("http://example.com", timeout=0.001) except ConnectionError as e: # This is the correct syntax print e r = "No response"
在这种情况下,您的程序将继续,设置其值r
通常会将响应保存到任何默认值