带有代理的 Python 请求导致 SSLError WRONG_VERSION_NUMBER [英] Python requests with proxy results in SSLError WRONG_VERSION_NUMBER

查看:78
本文介绍了带有代理的 Python 请求导致 SSLError WRONG_VERSION_NUMBER的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在 Python 中使用不同的代理.

我的代码:

导入请求代理 = {"https":'https://154.16.202.22:3128',http":'http://154.16.202.22:3128'}r=requests.get('https://httpbin.org/ip', proxies=proxies)打印(r.json())

我得到的错误是:

<预><代码>...引发 MaxRetryError(_pool, url, error 或 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)')))在处理上述异常的过程中,又发生了一个异常:回溯(最近一次调用最后一次):...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 但它没有用.

为什么这不起作用?

解决方案

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

$ https_proxy=http://154.16.202.22:3128 curl -v https://httpbin.org/ip* 正在尝试 154.16.202.22...* TCP_NODELAY 设置* 连接到 (nil) (154.16.202.22) 端口 3128 (#0)* 建立 HTTP 代理隧道到 httpbin.org:443>连接 httpbin.org:443 HTTP/1.1>主持人:httpbin.org:443>用户代理:curl/7.52.1>代理连接:保持活动><HTTP/1.1 400 错误请求

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

$ https_proxy=https://154.16.202.22:3128 curl -v https://httpbin.org/ip* 正在尝试 154.16.202.22...* TCP_NODELAY 设置* 连接到 (nil) (154.16.202.22) 端口 3128 (#0)* ALPN,提供http/1.1* 密码选择:ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH* TLSv1.2 (OUT)、TLS 标头、证书状态 (22):* TLSv1.2 (OUT), TLS 握手, 客户端问候 (1):* 错误:140770FC:SSL 例程:SSL23_GET_SERVER_HELLO:未知协议* curl_http_done: 称为过早 == 0* 关闭连接 0curl:(35) 错误:140770FC:SSL 例程:SSL23_GET_SERVER_HELLO:未知协议

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

I can't use the different proxy in Python.

My code:

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

The error I'm getting is:

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

I executed pip install requests.

I executed pip uninstall pyopenssl, then tried to pip install an old version of of pyopenssl but it didn't work.

Why is this not working?

解决方案

The proxy you use simply does not support proxying https:// URLs:

$ 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

Apart from that the URL for the proxy itself is wrong - it should be http://.. and not https://.. even if you proxy HTTPS traffic. But requests actually ignores the given protocol completely, so this error is not the reason for the problem. But just to demonstrate that it would not work either if the proxy itself got accessed with HTTPS (as the URL suggests):

$ 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

So the fix here would be to use a different proxy, one which actually supports proxying https:// URLs.

这篇关于带有代理的 Python 请求导致 SSLError WRONG_VERSION_NUMBER的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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