python请求中的OpenSSL错误

在带有 OpenSSL 版本 1.1.1i 和 pyopenssl 版本 1.1.1i 的 arch linux 上运行 python 版本 3.9.1 尝试将 https 代理与 requests 模块一起使用时出现以下错误:

    Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/urllib3/connectionpool.py", line 696, in urlopen
    self._prepare_proxy(conn)
  File "/usr/lib/python3.9/site-packages/urllib3/connectionpool.py", line 964, in _prepare_proxy
    conn.connect()
  File "/usr/lib/python3.9/site-packages/urllib3/connection.py", line 359, in connect
    conn = self._connect_tls_proxy(hostname, conn)
  File "/usr/lib/python3.9/site-packages/urllib3/connection.py", line 496, in _connect_tls_proxy
    return ssl_wrap_socket(
  File "/usr/lib/python3.9/site-packages/urllib3/util/ssl_.py", line 424, in ssl_wrap_socket
    ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls)
  File "/usr/lib/python3.9/site-packages/urllib3/util/ssl_.py", line 466, in _ssl_wrap_socket_impl
    return ssl_context.wrap_socket(sock)
  File "/usr/lib/python3.9/ssl.py", line 500, in wrap_socket
    return self.sslsocket_class._create(
  File "/usr/lib/python3.9/ssl.py", line 1040, in _create
    self.do_handshake()
  File "/usr/lib/python3.9/ssl.py", line 1309, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1123)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/requests/adapters.py", line 439, in send
    resp = conn.urlopen(
  File "/usr/lib/python3.9/site-packages/urllib3/connectionpool.py", line 755, in urlopen
    retries = retries.increment(
  File "/usr/lib/python3.9/site-packages/urllib3/util/retry.py", line 573, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='google.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1123)')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.9/site-packages/requests/api.py", line 76, in get
    return request('get', url, params=params, **kwargs)
  File "/usr/lib/python3.9/site-packages/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python3.9/site-packages/requests/sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python3.9/site-packages/requests/sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python3.9/site-packages/requests/adapters.py", line 514, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='google.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1123)')))

我正在运行的代码是:

import requests

proxy = {
    'https' : 'https://proxyip:proxyport'
}

requests.get("https://google.com", proxies=proxy)

无论我尝试什么 https 代理,我都会遇到同样的错误。我还重新安装了 openssl 和 python,没有任何变化。有什么建议么?

stack overflow OpenSSL errors in python requests
原文答案
author avatar

接受的答案

... line 496, in _connect_tls_proxy

您的代码正在尝试使用(新)支持通过 HTTPS 访问代理本身。这样做是因为您已明确将该 URL 作为代理指定为 https://... 而不是 http://...

'https' : 'https://proxyip:proxyport'
           ^^^^^^

代理本身很可能不支持到代理的 TLS 连接。通常,HTTP 代理仅与代理有纯 HTTP 连接。他们仍然可以通过这种方式代理 HTTPS 流量,因为客户端只需向代理发出 CONNECT 请求以创建隧道,然后在客户端和服务器之间使用端到端 TLS。

通过 HTTPS 访问代理将在客户端和代理之间添加一个额外的层 TLS,大多数代理不支持。因此,您可能需要普通的 HTTP 代理:

 'https' : 'http://proxyip:proxyport'
           ^^^^^^

请注意,在旧版本的请求库中,使用 http://https:// 的访问都有效。这些旧版本不支持到代理的 HTTPS,即使指定了 https:// ,也只是使用纯 HTTP。


答案:

作者头像

[enter image description here ](https://i.stack.imgur.com/8x6rm.png

添加 login.microsoftonline.com;management.azure.com ,因为例外将起作用。

作者头像

通过以下命令修复了我的情况:

 python3 -m pip install urllib3==1.22