【发布时间】:2021-06-23 08:16:34
【问题描述】:
我构建了一个 API(使用 flask-restful),将数据存储在其缓存中并将其公开给其他应用程序。当我尝试从另一个应用程序(也是烧瓶)向此 API 发送获取请求时,它返回以下错误
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/urllib3/connection.py", line 170, in _new_conn
(self._dns_host, self.port), self.timeout, **extra_kw
File "/usr/local/lib/python3.6/dist-packages/urllib3/util/connection.py", line 73, in create_connection
for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
File "/usr/lib/python3.6/socket.py", line 745, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/urllib3/connectionpool.py", line 706, in urlopen
chunked=chunked,
File "/usr/local/lib/python3.6/dist-packages/urllib3/connectionpool.py", line 382, in _make_request
self._validate_conn(conn)
File "/usr/local/lib/python3.6/dist-packages/urllib3/connectionpool.py", line 1010, in _validate_conn
conn.connect()
File "/usr/local/lib/python3.6/dist-packages/urllib3/connection.py", line 353, in connect
conn = self._new_conn()
File "/usr/local/lib/python3.6/dist-packages/urllib3/connection.py", line 182, in _new_conn
self, "Failed to establish a new connection: %s" % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x7f6f965d9358>: Failed to establish a new connection: [Errno -2] Name or service not known
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/urllib3/util/retry.py", line 574, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='data-collector.cloud', port=443): Max retries exceeded with url: /sample_url (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f6f965d9358>: Failed to establish a new connection: [Errno -2] Name or service not known',))
我认为发生此错误是因为我向 API 发送了太多具有相同 url 的请求。然后我通过添加限制了 API 调用的数量
decorators = [index.limiter.limit("60/minute")] API。然而,错误仍然存在。然后我认为该错误可能是由服务器接受的调用量引起的。我认为在进行 API 调用后我没有正确关闭连接。所以我加了
from requests.packages.urllib3 import Retry, PoolManager
retries = Retry(connect=5, read=2, redirect=5)
with PoolManager(retries=retries) as http:
response = http.request('GET', url)
但这也没有解决我的问题。我在这里想念什么?我正在使用python3.8
编辑:我发现它本身不是导致它的查询,因为如果我尝试其他查询,则会弹出相同的消息。我仍然不知道如何调试这个:/
【问题讨论】:
-
解决方案:确实是 DNS 问题。我必须指定应用程序的环境变量名称,而不是 data-collector.cloud。
-
你是如何指定环境变量名的?介意带我走几步吗?谢谢!
标签: python api urllib httpurlconnection flask-restful