Python 请求 HTTPConnectionPool 和最大重试次数超过 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24045265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:53:12  来源:igfitidea点击:

Python Requests HTTPConnectionPool and Max retries exceeded with url

pythonpython-requests

提问by Henry Thornton

On a Linux cluster, I get this error with Requests:

在 Linux 集群上,我在请求中收到此错误:

ConnectionError: HTTPConnectionPool(host='andes-1-47', port=8181): Max retries exceeded with url: /jammy/api/v1 (Caused by : '')

ConnectionError: HTTPConnectionPool(host='andes-1-47', port=8181): Max retries exceeded with url: /jammy/api/v1 (Caused by : '')

What does this error mean? Is it a Requests problem or is it on the host, and what is the solution?

这个错误是什么意思?是请求问题还是主机上的问题,解决方法是什么?

By the way, the program works successfully on both Windows and Linux standalone machines with localhost.

顺便说一下,该程序在带有 localhost 的 Windows 和 Linux 独立机器上都可以成功运行。

采纳答案by Ian Stapleton Cordasco

So the Max retries exceeded with url: ...bit can be vastly confusing. In all likelihood (since you mention that this works using localhost) that this is an application that you're deploying somewhere. This would also explain why the host name is andes-1-47and not something most would expect (e.g., example.com). My best guess is that you need to either use the IP address for andes-1-47(e.g., 192.168.0.255) or your linux cluster doesn't know how to resolve andes-1-47and you should add it to your /etc/hostsfile (i.e., adding the line: 192.168.0.255 andes-1-47).

所以这个Max retries exceeded with url: ...位可能会非常混乱。很可能(因为您提到这可以使用 localhost 工作)这是您在某处部署的应用程序。这也可以解释为什么主机名是andes-1-47而不是大多数人所期望的(例如,example.com)。我最好的猜测是您需要使用andes-1-47(例如,192.168.0.255)的 IP 地址,或者您的 linux 集群不知道如何解析andes-1-47,您应该将其添加到您的/etc/hosts文件中(即,添加行:)192.168.0.255 andes-1-47

If you want to see if your linux cluster can resolve the name you can always use this script:

如果您想查看您的 linux 集群是否可以解析名称,您可以随时使用此脚本:

import socket

socket.create_connection(('andes-1-47', 8181), timeout=2)

This will timeout in 2 seconds if you cannot resolve the hostname. (You can remove the timeout but it may take a lot longer to determine if the hostname is reachable that way.)

如果您无法解析主机名,这将在 2 秒后超时。(您可以删除超时,但确定主机名是否可以通过这种方式访问​​可能需要更长的时间。)

回答by johntellsall

in the urlopencall, try setting retries=Falseor retries=1to see the difference. The default is 3, which sounds quite reasonable.

urlopen通话中,尝试设置retries=Falseretries=1查看差异。默认是3,这听起来很合理。

http://urllib3.readthedocs.org/en/latest/pools.html#urllib3.connectionpool.HTTPConnectionPool.urlopen

http://urllib3.readthedocs.org/en/latest/pools.html#urllib3.connectionpool.HTTPConnectionPool.urlopen