【问题标题】:Python requests with proxy results in SSLError WRONG_VERSION_NUMBER带有代理的 Python 请求导致 SSLError WRONG_VERSION_NUMBER
【发布时间】:2021-03-05 16:43:21
【问题描述】:

我不能在 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()) 

我得到的错误是:

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

我执行了pip install requests

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

为什么这不起作用?

【问题讨论】:

    标签: python ssl proxy python-requests


    【解决方案1】:

    您使用的代理根本不支持代理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
    

    除了代理本身的 URL 是错误的 - 它应该是 http://.. 而不是 https://.. 即使您代理 HTTPS 流量。但是 requests 实际上完全忽略了给定的协议,所以这个错误不是问题的原因。但只是为了证明如果代理本身被 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
    

    所以这里的解决方法是使用不同的代理,它实际上支持代理https:// URL。

    【讨论】:

    • 感谢您的回答史蒂芬。你说我们应该使用不同的代理,但我们怎么知道使用哪一个?
    • @Scinana: “我们怎么知道要使用哪一个?...” - 你不能指望你碰巧在互联网上找到的任意免费代理可以工作你。根据您的需要,您可以设置自己的代理,使用付费服务...
    【解决方案2】:

    问题是由于最新的 urllib3 中的错误(我在版本 1.26.3 中发现的)。尝试通过pip3 install urllib3==1.23 降级到1.23,应该可以解决问题。

    【讨论】:

    • 这个答案对我有帮助。谢谢。
    • 您是否有更多关于这是一个错误的信息(问题链接等)?最近的版本中有一些代理更改,我正在尝试确定这是一个错误还是它的使用需要更改 - github.com/urllib3/urllib3/pull/1923
    • 这个问题可能值得一试 - github.com/urllib3/urllib3/issues/2075
    • 如果我理解正确的话,以前版本的 urllib3 似乎掩盖了错误的使用。对于较新的版本,不正确的使用不再有效。根据我之前的评论,以下内容可能会有所帮助(对我来说):github.com/urllib3/urllib3/issues/2075
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-19
    • 2021-06-01
    • 2022-01-19
    • 2021-04-07
    • 2015-04-24
    • 2016-02-05
    相关资源
    最近更新 更多