Python请求库遇到重试限制时如何访问服务器响应 [英] How to access server response when Python requests library encounters the retry limit
问题描述
我正在使用 Python 请求库来实现重试逻辑.这是我制作的一个简单脚本,用于重现我遇到的问题.在我们用完重试的情况下,我希望能够记录至少一个来自服务器的响应以帮助调试.但是,我不清楚如何访问该信息.当然,我可以通过其他方式实现重试来实现我的目标,但这似乎不是一个极端情况,我会惊讶地发现请求不支持我的用例.
我查看了 requests.exceptions.RetryError、它包装的 requests.packages.urllib3.exceptions.MaxRetryError 以及包装所有无济于事的 requests.packages.urllib3.exceptions.ResponseError.
我错过了什么吗?
#!/usr/bin/env python进口请求从 requests.adapters 导入 HTTPAdapter从 requests.packages.urllib3.util.retry 导入重试从 requests.exceptions 导入 RetryError定义主():retry_policy = 重试(总计=3,status_forcelist=[418])session = requests.Session()session.mount('http://', HTTPAdapter(max_retries=retry_policy))尝试:session.get('http://httpbin.org/status/418')除了作为 retry_error 的 RetryError:打印重试错误打印 retry_error.response 是 None如果 __name__ == '__main__':主要的()
<块引用>
$ python test.py
HTTPConnectionPool(host='httpbin.org', port=80): Max retries exceeded with url:/status/418 (Caused by ResponseError('too many 418 error Responds',))真的
查看 urllib3 源代码,我发现 Retry
对象采用名为 raise_on_status
的命名参数.True
是默认值.当设置为 False
时,遇到重试限制会导致返回响应而不是抛出异常.下面的代码向我展示了使用 Response
上的 raise_for_status
方法引发的 HTTPError
(包含 Response
)code>,但也可以直接使用 Response
.
#!/usr/bin/env python进口请求从 requests.adapters 导入 HTTPAdapter从 requests.packages.urllib3.util.retry 导入重试从 requests.exceptions 导入 HTTPError定义主():retry_policy = 重试(总计=3,status_forcelist=[418],raise_on_status=False)session = requests.Session()session.mount('http://', HTTPAdapter(max_retries=retry_policy))尝试:response = session.get('http://httpbin.org/status/418')response.raise_for_status()除了 HTTPError 为 e:打印 e.response.status_code打印 e.response.content如果 __name__ == '__main__':主要的()
<块引用>
$ python test.py
418-=[茶壶]=-_...._.'_ _`.|."`^`"._,\_;`"---"`|//|;/\_ _/`"""`
I am using the Python requests library to implement retry logic. Here is a simple script I made to reproduce the problem that I am having. In the case where we run out of retries, I would like to be able to log at least one of the responses from the server to help debugging. However, it is not clear to me how to access that information. Of course, I could implement retries in some other way to accomplish my goal, but it seemed like this wasn't that much of an edge case and I would be surprised to find out that requests did not support my use case.
I have looked at the requests.exceptions.RetryError, the requests.packages.urllib3.exceptions.MaxRetryError that it wraps, and the requests.packages.urllib3.exceptions.ResponseError that that wraps all to no avail.
Am I missing something?
#!/usr/bin/env python
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
from requests.exceptions import RetryError
def main():
retry_policy = Retry(
total=3,
status_forcelist=[418])
session = requests.Session()
session.mount('http://', HTTPAdapter(max_retries=retry_policy))
try:
session.get('http://httpbin.org/status/418')
except RetryError as retry_error:
print retry_error
print retry_error.response is None
if __name__ == '__main__':
main()
$ python test.py
HTTPConnectionPool(host='httpbin.org', port=80): Max retries exceeded with url: /status/418 (Caused by ResponseError('too many 418 error responses',))
True
Checking out the urllib3 source, I discovered that the Retry
object takes a named parameter called raise_on_status
. True
is the default. When set to False
, running into the retry limit causes a response to be returned instead of an exception being thrown. The code below shows me causing an HTTPError
(containing the Response
) to be thrown by using the raise_for_status
method on the Response
, but one could just as easily use the Response
directly.
#!/usr/bin/env python
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
from requests.exceptions import HTTPError
def main():
retry_policy = Retry(
total=3,
status_forcelist=[418],
raise_on_status=False)
session = requests.Session()
session.mount('http://', HTTPAdapter(max_retries=retry_policy))
try:
response = session.get('http://httpbin.org/status/418')
response.raise_for_status()
except HTTPError as e:
print e.response.status_code
print e.response.content
if __name__ == '__main__':
main()
$ python test.py
418
-=[ teapot ]=-
_...._
.' _ _ `.
| ."` ^ `". _,
\_;`"---"`|//
| ;/
\_ _/
`"""`
这篇关于Python请求库遇到重试限制时如何访问服务器响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!