python请求中的OpenSSL错误

Posted

技术标签:

【中文标题】python请求中的OpenSSL错误【英文标题】:OpenSSL errors in python requests 【发布时间】:2022-01-23 22:56:34 【问题描述】:

在带有 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,没有任何变化。有什么建议吗?

【问题讨论】:

你试过用 http 代替 https:proxy = 'https' : 'http://proxyip:proxyport' 【参考方案1】:
... 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。

【讨论】:

哦,伙计,这么好的一点!!!经过数小时的谷歌搜索并与此错误作斗争:requests.exceptions.SSLError: HTTPSConnectionPool(host='xxxx.com', port=443): Max retries exceeded with url:xxxx (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1123)')))我只是通过更改proxies = "https": "http://proxy-xxxx:8080", 谢谢一百万!【参考方案2】:

通过这个命令修复了我的情况:

 python3 -m pip install urllib3==1.22

【讨论】:

这基本上是降级到旧版本的 urllib,它不支持通过 HTTPS 访问代理,因此忽略给定的协议。真正更简单的解决方法是更正代理设置 - 从 https://..http://..

以上是关于python请求中的OpenSSL错误的主要内容,如果未能解决你的问题,请参考以下文章

加密技术---openss 理论

ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib?

04.openssl背景

macOS Big Sur下pecl安装swoole报需要openssl库解决方法

Openssl证书工具使用手册

openssl基础