简体   繁体   English

python 请求中的 OpenSSL 错误

[英]OpenSSL errors in python requests

Running python version 3.9.1 on arch linux with OpenSSL version 1.1.1i and pyopenssl version 1.1.1i I get the following error when trying to use an https proxy with the requests module: Running python version 3.9.1 on arch linux with OpenSSL version 1.1.1i and pyopenssl version 1.1.1i I get the following error when trying to use an https proxy with the requests module:

    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)')))

The code I am running is:我正在运行的代码是:

import requests

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

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

No matter what https proxy I try, I get the same error.无论我尝试什么 https 代理,我都会遇到同样的错误。 I have also reinstalled both openssl and python with no change.我还重新安装了 openssl 和 python 没有任何变化。 Any suggestions?有什么建议么?

... line 496, in _connect_tls_proxy

Your code is trying to use the (new) support for accessing the proxy itself over HTTPS.您的代码正在尝试使用(新)支持通过 HTTPS 访问代理本身。 This is done because you've explicitly given that URL as the proxy as https://... and not http://... :这样做是因为您已明确将 URL 作为https://...而不是http://...的代理:

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

It is very likely that the proxy itself does not support TLS connections to the proxy.代理本身很可能不支持到代理的 TLS 连接。 Commonly HTTP proxies have a plain HTTP connections to the proxy only.通常 HTTP 代理只有一个普通的 HTTP 连接到代理。 They still can proxy HTTPS traffic this way, since the client will simply issue a CONNECT request to the proxy to create a tunnel and then use end-to-end TLS between client and server.他们仍然可以通过这种方式代理 HTTPS 流量,因为客户端将简单地向代理发出 CONNECT 请求以创建隧道,然后在客户端和服务器之间使用端到端 TLS。

Accessing a proxy by HTTPS will add an additional layer of TLS between client and proxy, which is not supported by most proxies.通过 HTTPS 访问代理将在客户端和代理之间添加额外的 TLS 层,大多数代理不支持。 Therefore, you likely need plain HTTP proxy instead:因此,您可能需要普通的 HTTP 代理:

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

Note that in older versions of the requests library both access with http:// and https:// worked.请注意,在较旧版本的请求库中,使用http://https://访问都有效。 These older versions had no support for HTTPS to the proxy and simply used plain HTTP even if https:// would be specified.这些旧版本不支持 HTTPS 到代理,即使指定https:// ,也只是使用普通的 HTTP。

it was fixed with my case by this command:通过以下命令修复了我的情况:

 python3 -m pip install urllib3==1.22

在此处输入图像描述

Add login.microsoftonline.com;management.azure.com as exceptions will work.添加login.microsoftonline.com;management.azure.com异常会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM