带有代理的 Python 请求导致 SSLError WRONG_VERSION_NUMBER

kor*_*epp 7 python ssl proxy python-requests

我不能在 Python 中使用不同的代理。

我的代码:

import requests

proxies = {
    "https":'https://154.16.202.22:3128',
    "http":'http://154.16.202.22:3128'
    }

r=requests.get('https://httpbin.org/ip', proxies=proxies)
print(r.json()) 
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

.
.
.
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='httpbin.org', port=443): Max retries exceeded with url: /ip (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1122)')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
 
  .
  .
  .
requests.exceptions.SSLError: HTTPSConnectionPool(host='httpbin.org', port=443): Max retries exceeded with url: /ip (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1122)')))
Run Code Online (Sandbox Code Playgroud)

我执行了pip install requests

我执行了pip uninstall pyopenssl,然后尝试pip install使用旧版本的 pyopenssl 但它没有用。

为什么这不起作用?

Ste*_*ich 9

您使用的代理根本不支持代理https://URL:

$ https_proxy=http://154.16.202.22:3128 curl -v https://httpbin.org/ip
*   Trying 154.16.202.22...
* TCP_NODELAY set
* Connected to (nil) (154.16.202.22) port 3128 (#0)
* Establish HTTP proxy tunnel to httpbin.org:443
> CONNECT httpbin.org:443 HTTP/1.1
> Host: httpbin.org:443
> User-Agent: curl/7.52.1
> Proxy-Connection: Keep-Alive
> 
< HTTP/1.1 400 Bad Request
Run Code Online (Sandbox Code Playgroud)

除此之外,代理本身的 URL 是错误的 -即使您代理 HTTPS 流量,它http://..也应该是错误的。https://..但请求实际上完全忽略了给定的协议,因此这个错误并不是问题的原因。但只是为了证明如果代理本身通过​​ HTTPS 访问(如 URL 所示),它也不起作用:

$ https_proxy=https://154.16.202.22:3128 curl -v https://httpbin.org/ip
*   Trying 154.16.202.22...
* TCP_NODELAY set
* Connected to (nil) (154.16.202.22) port 3128 (#0)
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* TLSv1.2 (OUT), TLS header, Certificate Status (22):
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
* Curl_http_done: called premature == 0
* Closing connection 0
curl: (35) error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
Run Code Online (Sandbox Code Playgroud)

因此,此处的修复方法是使用不同的代理,该代理实际上支持代理https://URL。


An *_* Se 6

问题是由于最新的 urllib3 中的错误引起的(我在 version 中发现了它1.26.3)。尝试降级到1.23via pip3 install urllib3==1.23,它应该可以解决问题。

  • 最新的工作版本可以像这样安装:`pip install urllib3==1.25.11` (2认同)