我正在使用python requests库请求一个带有这种代码的API:
api_request = requests.get(f"http://data.api.org/search?q=example&ontologies=BFO&roots_only=true",
headers={'Authorization': 'apikey token=' + 'be03c61f-2ab8'})
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或其他地方找到我的问题的答案。
提前感谢您的时间和关注。
2条答案
按热度按时间ltqd579y1#
或
这是另一条路
Python-Requests close http connection
谢谢
session.mount('http://', requests.adapters.HTTPAdapter(max_retries=100))
6vl6ewon2#
查询无/错误输出会导致错误
当我使用正确的参数在容器中运行代码时,问题中的错误消失了。之前,有一个参数中的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)追溯(最近一次调用):File“/usr/lib/python2.7/logging/handlers.py“,line 556,in emit self.send(s)File“/usr/local/lib/python2.7/dist-packages/graypy/handler.py“,line 37,in send DatagramHandler.send(self,s)File“/usr/lib/python2.7/logging/handlers.py“,line 607,in send self.sock.sendto(s,(self.host,self.port))gaierror:[Errno -3]名称解析暂时失败
在运行结束时进一步向下:
看起来好像sqlalchemy无法处理来自查询的 None 输出,这将关闭连接,并且它会尝试并再次尝试访问它,直到连接在x次尝试后关闭。
其他调试步骤您可能会给予机会
我是初学者,不要相信这个答案。我仍然敢。
“名称解析暂时失败”意味着您无法访问网络中的服务器,无论是您的主机,您的DNS,您登录的地方,还是云。
docker ps
),或者干扰模块的网络流量。但如果它只是偶尔发生,并且在大部分相同的数据负载中开关,您可以尝试以简单的方式调试它: