requests.exceptions.SSLError: HTTPSConnectionPool(host=‘.*.‘, port=443): 开启代理时无法使用python的requests库

requests.exceptions.SSLError: HTTPSConnectionPool(host=‘.*.‘, port=443): 开启代理时无法使用python的requests库_第1张图片

python的requests,开启v2rayN后,无法使用python的requests库报错:

Traceback (most recent call last):
  File "E:\Dpandata\Myproject\Sjj1024\venv\lib\site-packages\urllib3\connection.py", line 174, in _new_conn
    conn = connection.create_connection(
  File "E:\Dpandata\Myproject\Sjj1024\venv\lib\site-packages\urllib3\util\connection.py", line 95, in create_connection
    raise err
  File "E:\Dpandata\Myproject\Sjj1024\venv\lib\site-packages\urllib3\util\connection.py", line 85, in create_connection
    sock.connect(sa)
TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "E:\Dpandata\Myproject\Sjj1024\venv\lib\site-packages\urllib3\connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
  File "E:\Dpandata\Myproject\Sjj1024\venv\lib\site-packages\urllib3\connectionpool.py", line 386, in _make_request
    self._validate_conn(conn)
  File "E:\Dpandata\Myproject\Sjj1024\venv\lib\site-packages\urllib3\connectionpool.py", line 1042, in _validate_conn
    conn.connect()
  File "E:\Dpandata\Myproject\Sjj1024\venv\lib\site-packages\urllib3\connection.py", line 358, in connect
    self.sock = conn = self._new_conn()
  File "E:\Dpandata\Myproject\Sjj1024\venv\lib\site-packages\urllib3\connection.py", line 186, in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: : Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "E:\Dpandata\Myproject\Sjj1024\venv\lib\site-packages\requests\adapters.py", line 489, in send
    resp = conn.urlopen(
  File "E:\Dpandata\Myproject\Sjj1024\venv\lib\site-packages\urllib3\connectionpool.py", line 787, in urlopen
    retries = retries.increment(
  File "E:\Dpandata\Myproject\Sjj1024\venv\lib\site-packages\urllib3\util\retry.py", line 592, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='www.google.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError(': Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "E:\Dpandata\Myproject\Sjj1024\src\tasks\test_waiwang.py", line 14, in
    response = requests.request("GET", url, headers=headers, data=payload)
  File "E:\Dpandata\Myproject\Sjj1024\venv\lib\site-packages\requests\api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
  File "E:\Dpandata\Myproject\Sjj1024\venv\lib\site-packages\requests\sessions.py", line 587, in request
    resp = self.send(prep, **send_kwargs)
  File "E:\Dpandata\Myproject\Sjj1024\venv\lib\site-packages\requests\sessions.py", line 701, in send
    r = adapter.send(request, **kwargs)
  File "E:\Dpandata\Myproject\Sjj1024\venv\lib\site-packages\requests\adapters.py", line 565, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='www.google.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError(': Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。'))

解决办法引用:开启代理时无法使用python的requests库 · Issue #2065 · 2dust/v2rayN · GitHub

有能力的也可以自己查看解决办法 

requests.exceptions.SSLError: HTTPSConnectionPool(host=‘.*.‘, port=443): 开启代理时无法使用python的requests库_第2张图片

我这里总结一下解决办法

如果你的请求不想走v2ray的设置:

设置 -> 参数设置 -> 系统代理设置 -> 下拉选中:socks={ip}:{socks_port}

requests.exceptions.SSLError: HTTPSConnectionPool(host=‘.*.‘, port=443): 开启代理时无法使用python的requests库_第3张图片

然后再发送请求:可以看到请求成功发送了 

requests.exceptions.SSLError: HTTPSConnectionPool(host=‘.*.‘, port=443): 开启代理时无法使用python的requests库_第4张图片

如果你的请求想走v2ray则设置为:

http=http://{ip}:{http_port};https=http://{ip}:{http_port}

requests.exceptions.SSLError: HTTPSConnectionPool(host=‘.*.‘, port=443): 开启代理时无法使用python的requests库_第5张图片

 

或者在python代码里添加proxy配置

先设置 -> 参数设置 -> 系统代理设置 -> 下拉选中:socks={ip}:{socks_port},然后允许来自局域网的连接:requests.exceptions.SSLError: HTTPSConnectionPool(host=‘.*.‘, port=443): 开启代理时无法使用python的requests库_第6张图片

 然后设置proxies = {'https': '127.0.0.1:10809'},注意端口,如果你的配置上本地监听端口为10808,则python的proxies里面要端口+1:

import requests

url = "https://www.google.com/"

payload = {}
headers = {
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36',
    'sec-ch-ua': '"Not_A Brand";v="99", "Google Chrome";v="109", "Chromium";v="109"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"macOS"'
}
proxies = {'https': '127.0.0.1:10809'}
response = requests.request("GET", url, headers=headers, data=payload, proxies=proxies)

发送请求之后,就可以了:

requests.exceptions.SSLError: HTTPSConnectionPool(host=‘.*.‘, port=443): 开启代理时无法使用python的requests库_第7张图片

你可能感兴趣的:(Python人生,ssl,网络协议,网络)