r = requests.get('http://example.com')
print str(r.status_code)
Run Code Online (Sandbox Code Playgroud)
我想检查我的服务器是否同时关闭或内部500错误。
如何使用请求检查两者?
根据引发请求失败的原因,请求会引发不同类型的异常。
import requests.exceptions
try:
r = requests.get('http://example.com')
r.raise_for_status() # Raises a HTTPError if the status is 4xx, 5xxx
except (requests.exceptions.ConnectionError, requests.exceptions.Timeout):
print "Down"
except requests.exceptions.HTTPError:
print "4xx, 5xx"
else:
print "All good!" # Proceed to do stuff with `r`
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3472 次 |
| 最近记录: |