请求.get()失败,即使插座插座().connect works(Python 3)

2024-09-30 05:14:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试了以下方法:

import requests
requests.get('https://www.google.com')

我一直在犯这样的错误:

gaierror: [Errno 11001] getaddrinfo failed

During handling of the above exception, another exception occurred:
NewConnectionError                        Traceback (most recent call last)
NewConnectionError: <requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x00000194F2F28780>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed

During handling of the above exception, another exception occurred:
MaxRetryError                             Traceback (most recent call last)
MaxRetryError: HTTPSConnectionPool(host='www.google.com', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x00000194F2F28780>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed',)))

During handling of the above exception, another exception occurred:
ProxyError                                Traceback (most recent call last)
ProxyError: HTTPSConnectionPool(host='www.google.com', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x00000194F2F28780>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed',)))

不过,当我尝试使用socket建立连接时,效果很好:

socket.getaddrinfo('127.0.0.1', 8080)
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('www.pythonlearn.com', 80))
mysock.send(b'GET http://www.pythonlearn.com/code/intro-short.txt HTTP/1.0\n\n')

答复:

62

请求和套接字建立连接的方式有区别吗?我认为requests.get()使用下面的套接字架构来发出任何请求。这个假设错了吗?你知道吗


Tags: tocomwwwgoogleexceptionsocketconnectionrequests

热门问题