python请求中的OpenSSL错误 [英] OpenSSL errors in python requests

查看:33
本文介绍了python请求中的OpenSSL错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 回溯(最近一次调用最后一次):文件/usr/lib/python3.9/site-packages/urllib3/connectionpool.py",第 696 行,在 urlopenself._prepare_proxy(conn)文件/usr/lib/python3.9/site-packages/urllib3/connectionpool.py",第 964 行,在 _prepare_proxy 中连接.connect()文件/usr/lib/python3.9/site-packages/urllib3/connection.py",第359行,在connectconn = self._connect_tls_proxy(主机名,conn)文件/usr/lib/python3.9/site-packages/urllib3/connection.py",第 496 行,在 _connect_tls_proxy返回 ssl_wrap_socket(文件/usr/lib/python3.9/site-packages/urllib3/util/ssl_.py",第 424 行,在 ssl_wrap_socketssl_sock = _ssl_wrap_socket_impl(袜子,上下文,tls_in_tls)文件/usr/lib/python3.9/site-packages/urllib3/util/ssl_.py",第466行,_ssl_wrap_socket_impl返回 ssl_context.wrap_socket(sock)文件/usr/lib/python3.9/ssl.py",第 500 行,在 wrap_socket返回 self.sslsocket_class._create(文件/usr/lib/python3.9/ssl.py",第 1040 行,在 _create 中self.do_handshake()文件/usr/lib/python3.9/ssl.py",第 1309 行,在 do_handshake 中self._sslobj.do_handshake()ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] 错误的版本号 (_ssl.c:1123)在处理上述异常的过程中,又发生了一个异常:回溯(最近一次调用最后一次):文件/usr/lib/python3.9/site-packages/requests/adapters.py",第439行,发送resp = conn.urlopen(文件/usr/lib/python3.9/site-packages/urllib3/connectionpool.py",第 755 行,在 urlopen重试 = 重试.增量(文件/usr/lib/python3.9/site-packages/urllib3/util/retry.py",第573行,增量引发 MaxRetryError(_pool, url, error 或 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)')))在处理上述异常的过程中,又发生了一个异常:回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中.文件/usr/lib/python3.9/site-packages/requests/api.py",第76行,在get返回请求('get', url, params=params, **kwargs)文件/usr/lib/python3.9/site-packages/requests/api.py",第61行,请求返回 session.request(method=method, url=url, **kwargs)文件/usr/lib/python3.9/site-packages/requests/sessions.py",第542行,请求resp = self.send(prep, **send_kwargs)文件/usr/lib/python3.9/site-packages/requests/sessions.py",第655行,发送r = 适配器.发送(请求,**kwargs)文件/usr/lib/python3.9/site-packages/requests/adapters.py",第514行,发送引发 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)')))

我正在运行的代码是:

导入请求代理 = {'https' : 'https://proxyip:proxyport'}requests.get("https://google.com", proxies=proxy)

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

解决方案

... 第 496 行,在 _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.

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. I have also reinstalled both openssl and python with no change. Any suggestions?

解决方案

... line 496, in _connect_tls_proxy

Your code is trying to use the (new) support for accessing the proxy itself over HTTPS. This is done because you've explicitly given that URL as the proxy as https://... and not http://...:

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

It is very likely that the proxy itself does not support TLS connections to the proxy. Commonly HTTP proxies have a plain HTTP connections to the proxy only. 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.

Accessing a proxy by HTTPS will add an additional layer of TLS between client and proxy, which is not supported by most proxies. Therefore, you likely need plain HTTP proxy instead:

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

Note that in older versions of the requests library both access with http:// and https:// worked. These older versions had no support for HTTPS to the proxy and simply used plain HTTP even if https:// would be specified.

这篇关于python请求中的OpenSSL错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆