gpt4 book ai didi

python - socket.gaierror : [Errno -2] Name or service not known | Python

转载 作者:行者123 更新时间:2023-12-03 07:38:15 25 4
gpt4 key购买 nike

我已关闭我的互联网连接,并想在代码中捕获它。我已经设置了超时限制,但是它不起作用。我收到大量错误消息,我不了解。

任何人都可以帮助您了解这些错误消息的含义吗?

我的python代码(简体):

import requests
from requests.exceptions import Timeout

url = 'https://api.github.com'
try:
resp = requests.get(url=url, timeout=7)
except Timeout:
print('The request timed out')
else:
entries = resp.json()
print(entries)

错误讯息:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 141, in _new_conn
(self.host, self.port), self.timeout, **extra_kw)
File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 60, 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/lib/python3/dist-packages/urllib3/connectionpool.py", line 601, in urlopen
chunked=chunked)
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 346, in _make_request
self._validate_conn(conn)
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 852, in _validate_conn
conn.connect()
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 284, in connect
conn = self._new_conn()
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 150, in _new_conn
self, "Failed to establish a new connection: %s" % e)
urllib3.exceptions.NewConnectionError: <urllib3.connection.VerifiedHTTPSConnection object at 0x7f1cea4779e8>: 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/lib/python3/dist-packages/requests/adapters.py", line 440, in send
timeout=timeout
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 639, in urlopen
_stacktrace=sys.exc_info()[2])
File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 398, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.github.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f1cea4779e8>: 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 "test.py", line 6, in <module>
resp = requests.get(url=url, timeout=7)
File "/usr/lib/python3/dist-packages/requests/api.py", line 72, in get
return request('get', url, params=params, **kwargs)
File "/usr/lib/python3/dist-packages/requests/api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 520, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 630, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python3/dist-packages/requests/adapters.py", line 508, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.github.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f1cea4779e8>: Failed to establish a new connection: [Errno -2] Name or service not known',))

最佳答案

socket.gaierror: [Errno -2] Name or service not known表示给定域(api.github.com)的名称解析失败。

名称解析(通常是DNS)可能由于各种原因而失败。当您断开Internet连接时,原因很明显(Name or service not known)。
gai实际上使用了glibc(GetAddressInfo)来执行名称解析,它是glibc的一部分(并且socket正在使用它,正如您可以想象的那样)。您实际上也可以在/etc/gai.conf中对其进行配置。

关于python - socket.gaierror : [Errno -2] Name or service not known | Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57234628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com