13

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?

Haem
  • 929
  • 6
  • 15
  • 31
kornepp
  • 133
  • 1
  • 1
  • 5

2 Answers2

21

Issue happens due to bug in latest urllib3(I've spotted it in version 1.26.3). Try downgrading to 1.23 via pip3 install urllib3==1.23, it should fix the problem.

An Se
  • 870
  • 11
  • 24
  • This answer helped me. Thank you. – marcin May 19 '21 at 12:29
  • Do you have any more information on this being a bug (issue link, etc)? There were some proxy changes in a recent release, and I'm trying to determine if it's a bug or the use of it needs to change - https://github.com/urllib3/urllib3/pull/1923 – loeschg Jun 23 '21 at 15:33
  • This issue may be worth checking out - https://github.com/urllib3/urllib3/issues/2075 – loeschg Jun 23 '21 at 20:58
  • If I understand this correctly, previous versions of urllib3 seemed to paper over incorrect use. With newer versions, incorrect use no longer works. Per my previous comment, the following may be helpful (was for me): https://github.com/urllib3/urllib3/issues/2075 – loeschg Jun 24 '21 at 16:17
  • 2
    The most recent working version can be installed like so: `pip install urllib3==1.25.11` – Theo F Jan 04 '22 at 15:57
  • Thanks a lot, would've never figured this one out myself! – Menno Van Dijk Jan 24 '22 at 17:12
  • While this maybe have originally been the truth, the bug has been fixed; anything nowadays means that the proxy you are using is not working correctly and needs to be addressed. – ZaxLofful Jan 26 '22 at 20:04
  • urllib3==1.25.11 works for me, but urllib3==1.23 didn't work – bjhoohaha Apr 19 '22 at 06:02
9

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.

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172
  • thanks for your answer Steffen. You say we should use a different proxy but how do we know which one to use? – Scinana Nov 01 '21 at 12:37
  • @Scinana: *"how do we know which one to use?... "* - You cannot expect an arbitrary free proxy you happen to find on the internet to work for you. Depending on what you want to do you can setup your own proxy, use paid services ... – Steffen Ullrich Nov 01 '21 at 17:01
  • The apache (actually libssl) error message: AH02003: SSL Proxy connect failed Library Error: error:0A00010B:SSL routines::wrong version number is one of the most misleading error messages ever. Do not start with debugging SSL/ TLS protocol versions. Always check first if we are talking to a plain unencrypted HTTP port. – Jürgen Weigert May 23 '23 at 13:17