title: Python SSL socket error: ssl.SSLWantReadError: The operation did not complete (read)
body: <p>I have a trivial server that uses SSL via a self-signed cert. I am trying to use the Python 3.4.2 SSL socket library to create a connection and return data via the following script with associated error:</p>
  
  <pre><code>import socket, ssl
  
  s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  
  ssl_socket = ssl.wrap_socket(s, keyfile="/path/to/server.pem", certfile="/path/to/client.pem", cert_reqs=ssl.CERT_REQUIRED, ssl_version=ssl.PROTOCOL_TLSv1_2, ca_certs="/path/to/client.pem")
  
  ssl_socket.connect(('hostname', port))
  ssl_socket.send("data_string".encode())
  # returns '4' (number of returned bytes)
  ssl_socket.setblocking(0) # turn off blocking
  ssl_socket.recv(4096)
  # error: ssl.SSLWantReadError: The operation did not complete (read) (_ssl.c:1960)
  </code></pre>
  
  <p>And if I don't set the blocking to 0, it will just hang. I've done enough research to see that it has to do with the size of the returned data, but I'm getting a return value of 4 bytes with I call <code>ssl_socket.send()</code>, so I'm not sure what I am missing.</p>
  
  <p>Note that I have a perl client that works properly as follows for context:</p>
  
  <pre><code>#!/usr/bin/env perl
  
  use IO::Socket::INET;
  use IO::Socket::SSL;
  
  # auto-flush on socket
  $| = 1;
  
  # create a connecting socket
  my $socket = new IO::Socket::SSL (
      PeerHost =&gt; 'hostname',
      PeerPort =&gt; '12345',
      Proto =&gt; 'tcp',
      SSL_cert_file =&gt; $ENV{'HOME'} . '/path/to/client.pem',
      SSL_key_file =&gt; $ENV{'HOME'} . '/path/to/server.pem',
  );
  die "cannot connect to the server $!\n" unless $socket;
  print "connected to the server\n";
  
  # data to send to a server
  my $req = 'data';
  print $socket "$req\n";
  my @r = ( &lt;$socket&gt; ) ;
  print "@r";
  
  $socket-&gt;close();
  </code></pre>
  
  <p>With output:</p>
  
  <pre><code>connected to the server
  {
     "password": "passwd",
     "username": "username"
  }
  </code></pre>
  
  <p>What is the proper way to use the Python SSL library to retrieve my requested data?</p>