Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
661 views
in Technique[技术] by (71.8m points)

python - Hotmail SSL3 version number error using smtp

I am trying to use the hotmail smtp server from python. However, my login attempt gives rise to an apparent SSL3 version number error. How can I change the version I am using and how do I even investigate this?

>> s.connect('smtp.live.com:587') 
(220,
 'BLU0-SMTP46.phx.gbl Microsoft ESMTP MAIL Service, Version: 6.0.3790.4675 ready at  Tue, 2 Jul 2013 12:15:57 -0700')
>> s.ehlo()
(250,
 'BLU0-SMTP46.phx.gbl Hello [123.456.789.01]
TURN
SIZE 41943040
ETRN
PIPELINING
DSN
ENHANCEDSTATUSCODES
8bitmime
BINARYMIME
CHUNKING
VRFY
TLS
STARTTLS
OK')
 s.starttls()
(220, '2.0.0 SMTP server ready')
>> s.login('[email protected]','MyPaSsW0rD')
---------------------------------------------------------------------------
SMTPServerDisconnected                    Traceback (most recent call last)
<ipython-input-48-c8e9d7577d8d> in <module>()
----> 1 s.login('[email protected]','myPassw0rd')

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.pyc in login(self, user, password)
    600         elif authmethod == AUTH_PLAIN:
    601             (code, resp) = self.docmd("AUTH",
--> 602                 AUTH_PLAIN + " " + encode_plain(user, password))
    603         elif authmethod == AUTH_LOGIN:
    604             (code, resp) = self.docmd("AUTH",

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.pyc in docmd(self, cmd, args)
    384         """Send a command, and return its response code."""
    385         self.putcmd(cmd, args)
--> 386         return self.getreply()
    387 
    388     # std smtp commands

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.pyc in getreply(self)
    357                 self.close()
    358                 raise SMTPServerDisconnected("Connection unexpectedly closed: "
--> 359                                              + str(e))
    360             if line == '':
    361                 self.close()

SMTPServerDisconnected: Connection unexpectedly closed: [Errno 1] _ssl.c:1363: error:1408F10B:SSL routines:SSL3_GET_RECORD:wrong version number

My SSL version:

>> import _ssl
>> print _ssl.OPENSSL_VERSION
OpenSSL 1.0.1e 11 Feb 2013

Perhaps this is related: Python Smtp SSL wrong version on linux

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I can duplicate your problem with Debian Wheezy with Python 2.7.3 which uses the exact same OpenSSL version you report. I captured packets with Wireshark and there is a successful TLS handshake and some data is exchanged. Shortly after that however, the client end gets unhappy with something the server sends and closes the connection.

I was able to work around the issue by using SSL3 instead of TLS. I couldn't figure out how to patch a library method in Python so that other libraries that used it would behave differently, so I just made my own local copy of smtplib.

I copied the 2.7 version of smtplib (click on the raw link to download) and changed one line:

        self.sock = ssl.wrap_socket(self.sock, keyfile, certfile)

to

        self.sock = ssl.wrap_socket(self.sock, keyfile, certfile, ssl_version=ssl.PROTOCOL_SSLv3)

Then with the edited file in my local directory I get:

Python 2.7.3 (default, Jan  2 2013, 13:56:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import smtplib
>>> s = smtplib.SMTP()
>>> s.connect('smtp.live.com:587') 
(220, 'BLU0-SMTP418.blu0.hotmail.com Microsoft ESMTP MAIL Service, Version: 6.0.3790.4675 ready at  Wed, 3 Jul 2013 09:59:32 -0700')
>>> s.ehlo()
(250, 'BLU0-SMTP418.blu0.hotmail.com Hello [24.143.227.254]
TURN
SIZE 41943040
ETRN
PIPELINING
DSN
ENHANCEDSTATUSCODES
8bitmime
BINARYMIME
CHUNKING
VRFY
TLS
STARTTLS
OK')
>>> s.starttls()
(220, '2.0.0 SMTP server ready')
>>> s.ehlo()
(250, 'BLU0-SMTP418.blu0.hotmail.com Hello [24.143.227.254]
TURN
SIZE 41943040
ETRN
PIPELINING
DSN
ENHANCEDSTATUSCODES
8bitmime
BINARYMIME
CHUNKING
VRFY
AUTH LOGIN PLAIN
OK')
>>> s.login('[email protected]','MyPaSsW0rD')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "smtplib.py", line 615, in login
    raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, '5.0.0 Authentication Failed')
>>> 

I don't have a valid Hotmail account so I can't get past here, but there is no longer an SSL error.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...