Python is a versatile programming language that is used all over the world for designing a variety of applications. The language is so popular that beginners are learning to design projects. You may land up in “ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1056)” error.
The error seems really something complex, but it is not as complex as it looks. In this article, we are going to guide you on how to tackle this error simply, but before that, you need to see how you get the error warning and what code you are compiling that makes the error pop up.
How does the “ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1056)” error occur
When trying to send an email using a script, you end up with an error. Let’s check out the code
server = smtplib.SMTP_SSL('smtp.mail.com', 587)
server.login("[email protected]", "password")
server.sendmail(
"[email protected]",
"[email protected]",
"email text")
server.quit()
After running this code, you get the error:
ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1056)
Now that you know how you get the error warning. Have a look at the solution to fix the error
Solutions to fix “ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1056)” error
Solution 1 – Use TLS over regular SMTP rather than ‘SMTP_SS’
This solution is effective when it comes to fixing “ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1056)” error. The SSL port is not 587, the correct one is 456. Your email will go to the junk if you are using SSL. Check out the following code to send mail
import smtplib, ssl
port = 587 # For starttls
smtp_server = "smtp.gmail.com"
sender_email = "[email protected]"
receiver_email = "[email protected]"
password = "your smtp email password"
message = """This Message is send from python script"""
context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
server.ehlo() # Can be omitted
server.starttls(context=context)
server.ehlo() # Can be omitted
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message)
Solution 2 – Mailbox Configuration
The cause of the error is the mail server connection error. Follow the below code to get the issue solved
smtplib.SMTP(self.host, self.port, timeout=300) [Use when TLS is turned off]
and you can also do this:
smtplib.SMTP_SSL(self.host, self.port, timeout=300). [Applied when TLS is activated]
for the 163 configuration of the mailbox, use the following code
host="smpt.163.com"
port="25" [do not use TLS]
the below code can also be placed
port="465" [utilize TLS]
This is the second solution that can help you fix the error.
Solution 3 – Trace mode
If the traceback is the issue that is causing the error to pop up, you need to run your program in the trace mode and report the output using the below code
$ HTTPX_LOG_LEVEL=trace python program.py
This solution is also an effective yet simple one to fix the SSL error.
Conclusion
Ants that’s how you can solve “ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1056)” error. It is on you to use the solution that is suitable for the program you are working on.
Don’t forget to drop a message to let us know your queries!
Reference Source: https://github.com/encode/httpx/issues/646