gpt4 book ai didi

python - Python : Handling requests exceptions the right way

转载 作者:行者123 更新时间:2023-12-03 09:02:43 25 4
gpt4 key购买 nike

我最近从urlib2切换到了请求,但不确定如何处理异常。什么是最佳做法?我当前的代码如下所示,但效果不佳:

try:
response = requests.get(url)
except requests.ConnectionError , e:
logging.error('ConnectionError = ' + str(e.code))
return False
except requests.HTTPError , e:
logging.error('HTTPError = ' + str(e.reason))
return False
except requests.Timeout, e:
logging.error('Timeout')
return False
except requests.TooManyRedirects:
logging.error('TooManyRedirects')
return False
except Exception:
import traceback
logging.error('generic exception: ' + traceback.format_exc())
return False

最佳答案

由于评论的效果不好,您是否尝试过:

try:
# some code
except Exception as e:
print e

关于python - Python : Handling requests exceptions the right way,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16986931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com