ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1002)

avatar
Borislav Hadzhiev

Last updated: Apr 13, 2024
3 min

banner

# Table of Contents

  1. ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1002)
  2. Use TLS instead of SSL
  3. Solving the SSL: WRONG_VERSION_NUMBER error when using requests
  4. Solving the error when using Django

# ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1002)

The Python "ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1002)" occurs when you incorrectly specify the port number when using SSL.

To solve the error, set the port to 465 when using SSL.

Here is an example of how the error occurs.

main.py
import smtplib # ๐Ÿ‘‡๏ธ Port set incorrectly (must be 465) port = 587 smtp_server = 'smtp.gmail.com' sender_email = 'sender@gmail.com' receiver_email = "receiver@gmail.com" password = 'dogsandcats123' message = '''\ Subject: Your Subject Your message contents.''' with smtplib.SMTP_SSL(smtp_server, port) as server: server.login(sender_email, password) server.sendmail( sender_email, receiver_email, message ) server.quit()

ssl error ssl wrong version number

I set the port to 587 when using the smtplib.SMTP_SSL() class which caused the error.

When connecting to an SMTP server with SSL encryption, set the port to 465.

main.py
import smtplib # โœ… Port correctly set to 465 port = 465 smtp_server = 'smtp.gmail.com' sender_email = 'sender@gmail.com' receiver_email = "receiver@gmail.com" password = 'dogsandcats123' message = '''\ Subject: Your Subject Your message contents.''' with smtplib.SMTP_SSL(smtp_server, port) as server: server.login(sender_email, password) server.sendmail( sender_email, receiver_email, message ) server.quit()

Set the port to 465 when calling the smtplib.SMTP_SSL() class to solve the error.

The smtplib.SMTP_SSL class takes the following 2 arguments:

  1. host - the name of the remote host to which to connect.
  2. port - the port to which to connect.

# Use TLS instead of SSL

When you use SSL encryption on port 465, you might run into issues, e.g. your mail being processed as spam.

The 465 port has recently been deprecated and its use is not recommended.

Instead, it is better to use TLS over port 587.

main.py
import smtplib import ssl # โœ… Set port to 587 port = 587 smtp_server = 'smtp.gmail.com' sender_email = 'sender@gmail.com' receiver_email = "receiver@gmail.com" password = 'dogsandcats123' message = '''\ Subject: Your Subject Your message contents.''' # โœ… Create the SSLContext object context = ssl.create_default_context() # โœ… Use smtplib.SMTP() class with smtplib.SMTP(smtp_server, port) as server: # โœ… Put the connection into TLS mode server.starttls(context=context) server.login(sender_email, password) server.sendmail( sender_email, receiver_email, message ) server.quit()

Notice that we set the port variable to 587.

The port is used for email submissions by a mail client to a mail server.

Port 587 requires authentication and encryption, so your mail is less likely to end in the spam folder.

The ssl.create_default_context() method creates an SSLContext object with the default settings.

main.py
context = ssl.create_default_context()

Notice that we used the smtplib.SMTP class and not smtplib.SMTP_SSL.

main.py
with smtplib.SMTP(smtp_server, port) as server:

The server.starttls() method call puts the connection into TLS mode.

main.py
server.starttls(context=context)

# Solving the SSL: WRONG_VERSION_NUMBER error when using requests

If you got the error when using requests or urllib3, try to upgrade your urllib3 version.

shell
pip install urllib3 --upgrade # Or with pip3 pip3 install urllib3 --upgrade

The error is caused because of a bug in an older urllib3 version.

Another thing you can try if you get the error when using the requests package with a proxy, set the https proxy URL to use the http protocol.

main.py
import requests # ๐Ÿ‘‡๏ธ Notice that it uses http:// protocol https_proxy = "http://127.0.0.1:5000" proxies = { "http": https_proxy, } request = requests.get( 'https://www.google.com', verify=False, proxies=proxies )

Notice that the https proxy uses the http protocol.

This is necessary because of a bug in urllib3.

# Solving the error when using Django

If you got the error when using Django, make sure:

  1. Your email backend is set to "django.core.mail.backends.smtp.EmailBackend".
settings.py
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"

The line should be in your settings.

  1. If you've set EMAIL_USE_TLS to True, then set EMAIL_PORT to 587.
settings.py
EMAIL_USE_TLS = True EMAIL_USE_SSL = False EMAIL_PORT = 587
  1. If you've set EMAIL_USE_SSL to True, then set EMAIL_PORT to 465.
settings.py
EMAIL_USE_SSL = True EMAIL_USE_TLS = False EMAIL_PORT = 465

Make sure that only one of EMAIL_USE_TLS and EMAIL_USE_SSL is set to True and set the corresponding port.

The port for TLS is 587 and the port for SSL is 465.

The error occurs if you get the port number wrong.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev