- SSL: WRONG_VERSION_NUMBER
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
- Python — SSL — wrong version number
- How to fix python requests with proxy results in sslerror wrong_version_number?
- Method 1: Upgrade to a newer version of Requests
- Method 2: Force a specific SSL version with the requests.Session object
- Method 3: Disable SSL verification for the request
- Method 4: Use a different proxy that supports the desired SSL version
- python 3 smtplib exception: ‘SSL: WRONG_VERSION_NUMBER’ logging in to outlook
SSL: WRONG_VERSION_NUMBER
Depending on your proxy settings, the proxy server can be to blame for this. If you have set an https_proxy to an https:// path the proxy server can interfere with the validation process of TLS. Changing this to an http://proxy.address allows the proxy server to open the connection, and then the TLS handshake to occur between host and destination.
It can be useful to try curl -v with similar settings to see what’s going on, as the python ssl bindings can misinterpret/obscure errors from the underlying library.
At the root of this, I speculate that a recent update in long-term stable images of distros like Debian has led to a raft of these issues emerging where they were perhaps previously invisible.
This fixed my problem under linux:
In my case it was caused by a typo in the URL:
If the URL has https protocol, python will try to perform an SSL handshake and the server won’t understand it. SSL library will get confused by the response and throw an error.
So, replacing the protocol did the trick:
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.26.43546
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Python — SSL — wrong version number
This will probably be just another unsolved thread but i’ll fill in some info anyway. I can’t get my SSL wrapping together not even for a second. Any ideas to what i’m doing wrong with my wrap_socket() and do_handshake()? The key files appear to be 100% perfect, and i’ve tried with AND without the .recv() before the handshake. That just generates these depending on where i put the recv():
class Server(): def __init__(self, listen = '', port = 8080, ssl = False): self.sock = socket.socket() self.sock.bind((listen, port)) self.sock.listen(5) def accept(self): newsocket, fromaddr = self.sock.accept() newsocket.recv(32) newsocket.setblocking(0) sslsock = ssl.wrap_socket(newsocket, server_side=True, certfile="./kernel/sock/server.crt", keyfile="./kernel/sock/server.key", cert_reqs=ssl.CERT_NONE, ssl_version=ssl.PROTOCOL_TLSv1, do_handshake_on_connect=False, suppress_ragged_eofs=True) sslsock.do_handshake() return sslsock, fromaddr
For the record, if it’s not obvious or i’m wrong, it’s the handshake that fails 🙂 I modified the code a bit, trying SSLv3 and also change the position of the wrapping a bit:
import socket, ssl, time, select class Server(): def __init__(self, listen = '', port = 443, ssl = False): self.sock = socket.socket() self.sock.bind((listen, port)) self.sock.listen(5) def accept(self): self.sock = ssl.wrap_socket(self.sock, server_side=True, certfile="./kernel/sock/server.crt", keyfile="./kernel/sock/server.key", cert_reqs=ssl.CERT_NONE, ssl_version=ssl.PROTOCOL_SSLv3, do_handshake_on_connect=False, suppress_ragged_eofs=True) newsocket, fromaddr = self.sock.accept() print [newsocket.recv(32)] newsocket.setblocking(False) newsocket.do_handshake() return newsocket, fromaddr s = Server() ns, na = s.accept() print ns.recv(1024)
ssl.SSLError: [Errno 1] _ssl.c:1331: error:140940E5:SSL routines:SSL3_READ_BYTES:ssl handshake failure
import socket, ssl, time, select from OpenSSL import SSL class Server(): def __init__(self, listen = '', port = 443, ssl = False): ctx = SSL.Context(SSL.SSLv23_METHOD) ctx.use_privatekey_file("server.pem") ctx.use_certificate_file("server.pem") self.sock = SSL.Connection(ctx, socket.socket()) self.sock.bind((listen, port)) self.sock.listen(5) def accept(self): newsocket, fromaddr = self.sock.accept() return newsocket, fromaddr s = Server() ns, na = s.accept() print ns.recv(1024)
import socket, ssl, time #, select class Server(): def __init__(self, listen = '', port = 443, ssl = False): self.sock = socket.socket() self.sock.bind((listen, port)) self.sock.listen(5) def accept(self): self.ssl_sock = None while not self.ssl_sock: self.ssl_sock = ssl.wrap_socket(self.sock, server_side=True, certfile=r"C:\moo.pem", keyfile=r"C:\moo.key", cert_reqs=ssl.CERT_NONE, ssl_version=ssl.PROTOCOL_TLSv1) newsocket, fromaddr = self.ssl_sock.accept() print([newsocket.recv()]) return newsocket, fromaddr s = Server() ns, na = s.accept() print(ns.recv(1024))
How to fix python requests with proxy results in sslerror wrong_version_number?
The Python Requests library is a powerful tool for making HTTP requests, but it can sometimes run into issues when working with a proxy. One common error that users may encounter is the «SSLError: WRONG_VERSION_NUMBER» error, which is raised when the client and server are using different SSL/TLS versions. This can occur for a variety of reasons, including mismatches in the versions supported by the client, the proxy, and the server, or issues with the SSL certificate being used. To resolve this error, several methods can be tried.
Method 1: Upgrade to a newer version of Requests
To fix the SSLError WRONG_VERSION_NUMBER issue with Python requests and proxy, the best solution is to upgrade to a newer version of Requests. Here are the steps to do it:
- Check the current version of Requests by running the following command in your terminal:
pip freeze | grep requests
pip install --upgrade requests
import requests proxies = 'http': 'http://user:pass@proxy:port', 'https': 'https://user:pass@proxy:port' > response = requests.get('https://www.example.com', proxies=proxies, verify='/path/to/certfile')
import requests proxies = 'http': 'http://user:pass@proxy:port', 'https': 'https://user:pass@proxy:port' > response = requests.get('https://www.example.com', proxies=proxies, verify=False)
That’s it! By upgrading to a newer version of Requests and specifying the SSL certificates to use, you can fix the SSLError WRONG_VERSION_NUMBER issue with Python requests and proxy.
Method 2: Force a specific SSL version with the requests.Session object
To fix the SSLError WRONG_VERSION_NUMBER when using Python requests with proxy, you can force a specific SSL version with the requests.Session object. Here are the steps:
import requests session = requests.Session()
session.mount('https://', requests.adapters.HTTPAdapter( max_retries=3, pool_connections=10, pool_maxsize=10, ssl_version=requests.utils.DEFAULT_SSL_CIPHER_LIST[-1]))
response = session.get('https://www.example.com')
print(response.status_code) print(response.content)
Here is the complete code:
import requests session = requests.Session() session.mount('https://', requests.adapters.HTTPAdapter( max_retries=3, pool_connections=10, pool_maxsize=10, ssl_version=requests.utils.DEFAULT_SSL_CIPHER_LIST[-1])) response = session.get('https://www.example.com') print(response.status_code) print(response.content)
This should fix the SSLError WRONG_VERSION_NUMBER when using Python requests with proxy.
Method 3: Disable SSL verification for the request
To fix the SSLError WRONG_VERSION_NUMBER error when using Python requests with a proxy, you can disable SSL verification for the request. Here are the steps to do this:
session.proxies = "http": "http://your-proxy-url", "https": "http://your-proxy-url" >
response = session.get("https://your-url.com")
Here is the complete code:
import requests session = requests.Session() session.verify = False session.proxies = "http": "http://your-proxy-url", "https": "http://your-proxy-url" > response = session.get("https://your-url.com")
This code will disable SSL verification for the request and use the specified proxy.
Method 4: Use a different proxy that supports the desired SSL version
To fix the SSLError WRONG_VERSION_NUMBER error with Python requests and a proxy, you can try using a different proxy that supports the desired SSL version. Here’s an example code snippet to achieve this:
import requests proxy_url = 'http://myproxy.com' proxy_port = '8080' ssl_version = 'TLSv1.2' url = 'https://www.example.com' proxies = 'http': f'proxy_url>:proxy_port>', 'https': f'proxy_url>:proxy_port>', 'ssl': f'ssl_version>' > response = requests.get(url, proxies=proxies) print(response.content)
In this code, we first define the proxy URL and port, as well as the desired SSL version. We then define the URL to make a request to, and create a proxy dictionary with the necessary information.
Finally, we make the request using the requests.get() method, passing in the URL and the proxy dictionary. The response content is then printed to the console.
Note that this is just one possible solution to the SSLError WRONG_VERSION_NUMBER error with Python requests and a proxy. There may be other methods or configurations that work better for your specific use case.
python 3 smtplib exception: ‘SSL: WRONG_VERSION_NUMBER’ logging in to outlook
The following code in python 3 raises an error on my computer, and I don’t know how to log in properly:
import smtplib connection = smtplib.SMTP('smtp-mail.outlook.com', 587) connection.ehlo() connection.starttls() connection.ehlo() connection.login('_these_dont_matter@outlook.com', '_the_error_persists_')
Traceback (most recent call last): File "/usr/lib/python3.3/smtplib.py", line 366, in getreply line = self.file.readline() File "/usr/lib/python3.3/socket.py", line 297, in readinto return self._sock.recv_into(b) File "/usr/lib/python3.3/ssl.py", line 460, in recv_into return self.read(nbytes, buffer) File "/usr/lib/python3.3/ssl.py", line 334, in read v = self._sslobj.read(len, buffer) ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1504) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "", line 1, in File "/usr/lib/python3.3/smtplib.py", line 621, in login AUTH_PLAIN + " " + encode_plain(user, password)) File "/usr/lib/python3.3/smtplib.py", line 398, in docmd return self.getreply() File "/usr/lib/python3.3/smtplib.py", line 370, in getreply + str(e)) smtplib.SMTPServerDisconnected: Connection unexpectedly closed: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1504)
The SMTP information (port, etc) I used is from a microsoft help site, other ports or domains for outlook I’ve tried result in the same error. The output of openssl version is 1.0.1e 11 Feb 2013