socket.gaierror:[Errno -3]名称解析暂时失败

问题描述 投票:0回答:2

我正在使用 python requests 库请求具有此类代码的 API:

api_request = requests.get(f"http://data.api.org/search?q=example&ontologies=BFO&roots_only=true",
                             headers={'Authorization': 'apikey token=' + '<MY_TOKEN>'})

api_result = api_request.json()
collection = api_result["collection"]
...

当我不请求大量内容时,此代码工作正常,但否则我会收到错误。奇怪的是我每次请求很多内容都得不到。错误信息如下:

Traceback (most recent call last):
  File "/home/nobu/.local/lib/python3.6/site-packages/urllib3/connection.py", line 160, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw
  File "/home/nobu/.local/lib/python3.6/site-packages/urllib3/util/connection.py", line 61, 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 -3] Temporary failure in name resolution

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/nobu/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 677, in urlopen
    chunked=chunked,
  File "/home/nobu/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 392, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/lib/python3.6/http/client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib/python3.6/http/client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.6/http/client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.6/http/client.py", line 1026, in _send_output
    self.send(msg)
  File "/usr/lib/python3.6/http/client.py", line 964, in send
    self.connect()
  File "/home/nobu/.local/lib/python3.6/site-packages/urllib3/connection.py", line 187, in connect
    conn = self._new_conn()
  File "/home/nobu/.local/lib/python3.6/site-packages/urllib3/connection.py", line 172, in _new_conn
    self, "Failed to establish a new connection: %s" % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7f4bdeca7080>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/nobu/.local/lib/python3.6/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/home/nobu/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 725, in urlopen
    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
  File "/home/nobu/.local/lib/python3.6/site-packages/urllib3/util/retry.py", line 439, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='data.api.org', port=80): Max retries exceeded with url: /ontologies/NCIT/classes/http%3A%2F%2Fncicb.nci.nih.gov%2Fxml%2Fowl%2FEVS%2FThesaurus.owl%23C48481/descendants (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f4bdeca7080>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "new_format.py", line 181, in <module>
    ontology_api(extraction(90))
  File "new_format.py", line 142, in ontology_api
    concept_extraction(collection)
  File "new_format.py", line 100, in concept_extraction
    api_request_tree = requests.get(f"{leaf}", headers={'Authorization': 'apikey token=' + f'{api_key}'})
  File "/home/nobu/.local/lib/python3.6/site-packages/requests/api.py", line 76, in get
    return request('get', url, params=params, **kwargs)
  File "/home/nobu/.local/lib/python3.6/site-packages/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/nobu/.local/lib/python3.6/site-packages/requests/sessions.py", line 530, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/nobu/.local/lib/python3.6/site-packages/requests/sessions.py", line 643, in send
    r = adapter.send(request, **kwargs)
  File "/home/nobu/.local/lib/python3.6/site-packages/requests/adapters.py", line 516, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='data.api.org', port=80): Max retries exceeded with url: /ontologies/NCIT/classes/http%3A%2F%2Fncicb.nci.nih.gov%2Fxml%2Fowl%2FEVS%2FThesaurus.owl%23C48481/descendants (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f4bdeca7080>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',))

我不知道是否会收到错误,因为如果是由于其他原因,我过度请求了 API。我无法在 SO 或其他地方找到我的问题的答案。

提前感谢您的时间和关注。

python python-3.x api python-requests
2个回答
2
投票
with requests.Session() as s:
    s.get('http://google.com')

with requests.get('http://httpbin.org/get', stream=True) as r:
    # Do something

这是另一种方式

Python-请求关闭http连接

但是谢谢你

session.mount('http://', requests.adapters.HTTPAdapter(max_retries=100))


0
投票

查询的无/错误输出可能会导致错误

当我在具有正确参数的容器中运行代码时,相关错误消失了。之前,其中一个参数中有一个 id,但它不在我运行代码的数据集中。含义:查询尝试在数据集中查找 id,但什么也没找到,然后没有输出,然后这个遗留容器报告:

/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/properties.py:1029: SAWarning:在 Product.product_attribute 上,“passive_deletes”通常是 配置为一对多、一对一、多对多关系 仅有的。 self._check_cascade_settings(self._cascade) 回溯(大多数 最近的调用最后):文件“/usr/lib/python2.7/logging/handlers.py”, 第 556 行,在发射中 self.send(s) 文件“/usr/local/lib/python2.7/dist-packages/graypy/handler.py”,第 37 行, 发送中 DatagramHandler.send(self, s) 文件“/usr/lib/python2.7/logging/handlers.py”,第 607 行,在 send 中 self.sock.sendto(s, (self.host, self.port)) gaierror: [Errno -3] 名称解析暂时失败

在运行结束时进一步向下:

    raise e
AutoReconnect: connection closed

看起来 sqlalchemy 无法处理查询的 None 输出,这会关闭连接,并且它会尝试再次尝试到达它,直到 x 尝试后连接关闭。

您可能会给予机会的其他调试步骤

我是初学者,不要相信这个答案。我还是敢。

“名称解析暂时失败”意味着您无法访问网络中的服务器,无论是您的主机、您的 DNS、您登录的位置还是云。

  • 第一步是对代码尝试访问的每台服务器以及网络的 DNS 执行 ping 操作,以查看这些名称是否有效。
  • 您可能有一个被遗忘的容器仍在运行,它会更改您的网络 (
    docker ps
    ) 或通过其网络流量干扰模块。

但是,如果这种情况仅偶尔发生,并且在大部分相同的数据负载下会打开和关闭,您可以尝试以简单的方式进行调试:

  • 例外并登录,是否可以在错误发生时访问所有服务器。
  • 通过在代码中注释掉日志记录来关闭它。长时间测试代码,看看是否会出现错误。日志记录会导致网络流量。 API 调用也是如此,但我猜问题是 Python 模块在记录的 API 调用出现峰值时无法处理竞争条件。
© www.soinside.com 2019 - 2024. All rights reserved.