[英]Unexpected requests.exceptions.ConnectionError
I was trying to run this code:我试图运行这段代码:
import requests
r = requests.get("https://upos-sz-mirrorkodo.bilivideoo1.com/")
But a BIG ERROR has raised.但是出现了一个大错误。
Traceback (most recent call last):
File "E:\Programming\Python\VSC\lib\site-packages\urllib3\connection.py", line 169, in _new_conn
conn = connection.create_connection(
File "E:\Programming\Python\VSC\lib\site-packages\urllib3\util\connection.py", line 73, in create_connection
for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
File "C:\Users\王子涵\AppData\Local\Programs\Python\Python38\lib\socket.py", line 918, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "E:\Programming\Python\VSC\lib\site-packages\urllib3\connectionpool.py", line 699, in urlopen
httplib_response = self._make_request(
File "E:\Programming\Python\VSC\lib\site-packages\urllib3\connectionpool.py", line 382, in _make_request
self._validate_conn(conn)
File "E:\Programming\Python\VSC\lib\site-packages\urllib3\connectionpool.py", line 1010, in _validate_conn
conn.connect()
File "E:\Programming\Python\VSC\lib\site-packages\urllib3\connection.py", line 353, in connect
conn = self._new_conn()
File "E:\Programming\Python\VSC\lib\site-packages\urllib3\connection.py", line 181, in _new_conn
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x0000026E92F17F40>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "E:\Programming\Python\VSC\lib\site-packages\requests\adapters.py", line 439, in send
resp = conn.urlopen(
File "E:\Programming\Python\VSC\lib\site-packages\urllib3\connectionpool.py", line 755, in urlopen
retries = retries.increment(
File "E:\Programming\Python\VSC\lib\site-packages\urllib3\util\retry.py", line 574, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='upos-sz-mirrorkodo.bilivideoo1.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x0000026E92F17F40>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "e:\Desktop\1.py", line 48, in <module>
r = get(
File "E:\Programming\Python\VSC\lib\site-packages\requests\api.py", line 76, in get
return request('get', url, params=params, **kwargs)
File "E:\Programming\Python\VSC\lib\site-packages\requests\api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "E:\Programming\Python\VSC\lib\site-packages\requests\sessions.py", line 542, in request
resp = self.send(prep, **send_kwargs)
File "E:\Programming\Python\VSC\lib\site-packages\requests\sessions.py", line 655, in send
r = adapter.send(request, **kwargs)
File "E:\Programming\Python\VSC\lib\site-packages\requests\adapters.py", line 516, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='upos-sz-mirrorkodo.bilivideoo1.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x0000026E92F17F40>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))
I tried to ping this host but failed, and Google didn't give useful advice.我试图 ping 这个主机但失败了,谷歌也没有给出有用的建议。
Could you please tell me why and how to deal with it?你能告诉我为什么以及如何处理它吗? Thanks a lot.
非常感谢。
I have also asked this question on Github at https://github.com/psf/requests/issues/6010 .我还在https://github.com/psf/requests/issues/6010的 Github 上问过这个问题。
This error indicates that the URL you specified is simply not reachable .此错误表明您指定的 URL 根本无法访问。 You said yourself that you cannot ping the host.
您自己说您无法ping通主机。 Therefore, this issue is not related to your code.
因此,此问题与您的代码无关。 If you try
requests.get("https://www.google.com/")
it will probably work fine.如果您尝试
requests.get("https://www.google.com/")
它可能会正常工作。
Thanks to all your watching and answers.感谢大家的关注和回答。
The only reason is that this site don't allow http request with header "Python-requests", which is the default setting of requests.唯一的原因是该站点不允许使用 header "Python-requests" 的 http 请求,这是请求的默认设置。
There's no typos in the url, even though it is strange to see a domain ends with "o1". url 中没有拼写错误,尽管看到域以“o1”结尾很奇怪。
After all, it is a fantastic security system, I know the website can return 403 error when the User-Agent seem like a spider, but how can it just don't return anything that make Python raise an error!毕竟,这是一个很棒的安全系统,我知道当 User-Agent 看起来像蜘蛛时网站可以返回 403 错误,但它怎么可能不返回任何使 Python 引发错误的东西!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.