title: Python 3.4 - Connect to imap server using only TLSv1
body: <p>I'm trying to connect to an imap mail server using only TLSv1 in Python 3.4.</p>
  
  <p>After much troubleshooting (most of which determined that the mail server only supports TLSv1), I've found that I can connect to the server using openssl:</p>
  
  <pre><code>openssl s_client -connect mail.calpoly.edu:993 -tls1
  </code></pre>
  
  <p>as well as with the sockets package in Python 2.7:</p>
  
  <pre><code>Python 2.7.6 (default, Mar 22 2014, 22:59:56)
  [GCC 4.8.2] on linux2
  Type "help", "copyright", "credits" or "license" for more information.
  &gt;&gt;&gt; import ssl
  &gt;&gt;&gt; import socket
  &gt;&gt;&gt;
  &gt;&gt;&gt; sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  &gt;&gt;&gt; ssl_sock = ssl.wrap_socket(sock=sock, ssl_version=ssl.PROTOCOL_TLSv1)
  &gt;&gt;&gt; ssl_sock.connect(('mail.calpoly.edu', 993))
  &gt;&gt;&gt; ssl_sock
  &lt;ssl.SSLSocket object at 0x7fbab6e7aed8&gt;
  </code></pre>
  
  <p>When I try to connect in Python 3.4, however, I get a Handshake error:</p>
  
  <pre><code>Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
  [GCC 4.8.2] on linux
  Type "help", "copyright", "credits" or "license" for more information.
  &gt;&gt;&gt; import ssl
  &gt;&gt;&gt; import socket
  &gt;&gt;&gt; 
  &gt;&gt;&gt; sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  &gt;&gt;&gt; ssl_sock = ssl.wrap_socket(sock=sock, ssl_version=ssl.PROTOCOL_TLSv1)
  &gt;&gt;&gt; ssl_sock.connect(('mail.calpoly.edu', 993))
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "/usr/lib/python3.4/ssl.py", line 841, in connect
      self._real_connect(addr, False)
    File "/usr/lib/python3.4/ssl.py", line 832, in _real_connect
      self.do_handshake()
    File "/usr/lib/python3.4/ssl.py", line 805, in do_handshake
      self._sslobj.do_handshake()
  ssl.SSLError: [SSL: SSLV3_ALERT_HANDSHAKE_FAILURE] sslv3 alert handshake failure (_ssl.c:598)
  </code></pre>
  
  <p>It seems like Python 3.4 tries to use sslv3 even though I tell it not to.</p>
  
  <p>Does anyone know why this is happening and how I can work around it?</p>
  
  <p>P.S. - I'll be using imaplib in the code that will interface with the server. I used sockets in this example to highlight that this doesn't seem to be just an issue with the imaplib package.</p>