简体   繁体   中英

New versions of Python hangs on imaplib.IMAP4_SSL()

I have application that import emails from client IMAP4 mailboxes on home.pl (I think it is popular provider in Poland). It worked very well up to Python 2.7.9 but with version 2.7.10 it hangs on read() in imaplib.IMAP4_SSL() .

On my Fedora 23 I have Python 3.4.3 and 2.7.10.

With Python 3.4.3 such code works as expected:

[mn] python3 -c "import imaplib; x=imaplib.IMAP4_SSL('imap.home.pl', 993); print('finish', x)"
finish <imaplib.IMAP4_SSL object at 0x7f80cfd31710>

But with Python 2.7.10 it hangs. I pressed Ctrl-C after few seconds:

[mn] python -c "import imaplib; x=imaplib.IMAP4_SSL('imap.home.pl', 993); print('finish', x)"
^CTraceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib64/python2.7/imaplib.py", line 1166, in __init__
    IMAP4.__init__(self, host, port)
  File "/usr/lib64/python2.7/imaplib.py", line 202, in __init__
    typ, dat = self.capability()
  File "/usr/lib64/python2.7/imaplib.py", line 374, in capability
    typ, dat = self._simple_command(name)
  File "/usr/lib64/python2.7/imaplib.py", line 1088, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/usr/lib64/python2.7/imaplib.py", line 910, in _command_complete
    typ, data = self._get_tagged_response(tag)
  File "/usr/lib64/python2.7/imaplib.py", line 1017, in _get_tagged_response
    self._get_response()
  File "/usr/lib64/python2.7/imaplib.py", line 929, in _get_response
    resp = self._get_line()
  File "/usr/lib64/python2.7/imaplib.py", line 1027, in _get_line
    line = self.readline()
  File "/usr/lib64/python2.7/imaplib.py", line 1189, in readline
    return self.file.readline()
  File "/usr/lib64/python2.7/socket.py", line 451, in readline
    data = self._sock.recv(self._rbufsize)
  File "/usr/lib64/python2.7/ssl.py", line 734, in recv
    return self.read(buflen)
  File "/usr/lib64/python2.7/ssl.py", line 621, in read
    v = self._sslobj.read(len or 1024)
KeyboardInterrupt

You can also test it with docker to see that it worked with older versions of Python:

[mn] sudo docker run --rm -it python:2.7.9 python -c "import imaplib; x=imaplib.IMAP4_SSL('imap.home.pl', 993); print('finish', x)"
('finish', <imaplib.IMAP4_SSL instance at 0x7f682422b2d8>)

[mn] sudo docker run --rm -it python:2.7.10 python -c "import imaplib; x=imaplib.IMAP4_SSL('imap.home.pl', 993); print('finish', x)"
^CTraceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python2.7/imaplib.py", line 1166, in __init__
    IMAP4.__init__(self, host, port)
  File "/usr/local/lib/python2.7/imaplib.py", line 202, in __init__
    typ, dat = self.capability()
  File "/usr/local/lib/python2.7/imaplib.py", line 374, in capability
    typ, dat = self._simple_command(name)
  File "/usr/local/lib/python2.7/imaplib.py", line 1088, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/usr/local/lib/python2.7/imaplib.py", line 910, in _command_complete
    typ, data = self._get_tagged_response(tag)
  File "/usr/local/lib/python2.7/imaplib.py", line 1017, in _get_tagged_response
    self._get_response()
  File "/usr/local/lib/python2.7/imaplib.py", line 929, in _get_response
    resp = self._get_line()
  File "/usr/local/lib/python2.7/imaplib.py", line 1027, in _get_line
    line = self.readline()
  File "/usr/local/lib/python2.7/imaplib.py", line 1189, in readline
    return self.file.readline()
  File "/usr/local/lib/python2.7/socket.py", line 451, in readline
    data = self._sock.recv(self._rbufsize)
  File "/usr/local/lib/python2.7/ssl.py", line 734, in recv
    return self.read(buflen)
  File "/usr/local/lib/python2.7/ssl.py", line 621, in read
    v = self._sslobj.read(len or 1024)
KeyboardInterrupt

The same way I tested that it hangs with Python 3.4.4.

I think it is a bug because it hangs instead of raising an exception with helpful information so I reported it: http://bugs.python.org/issue26375

How can I workaround this problem?

This is a server issue, it is not responding to the CAPABILITY command (< and > added for clarity):

$ socat readline openssl:imap.home.pl:993,crlf,verify=0 
< * OK imap.home.pl IdeaImapServer v0.80 ready
> tag CAPABILITY

And it sits there for a very long time. There may be an issue with the server.

Strangely, sending two commands back to back kicks it:

< a CAPABILITY
< b CAPABILITY
> * CAPABILITY IMAP4rev1 LITERAL+ CHILDREN I18NLEVEL=1 IDLE SORT UIDPLUS UNSELECT WITHIN XLIST AUTH=PLAIN AUTH=LOGIN
> a OK Completed
> * CAPABILITY IMAP4rev1 LITERAL+ CHILDREN I18NLEVEL=1 IDLE SORT UIDPLUS UNSELECT WITHIN XLIST AUTH=PLAIN AUTH=LOGIN
> b OK Completed

There is a serious parsing bug in the server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM