Search code examples
python-3.xsocketssslblockingrecv

Python SSL socket error: ssl.SSLWantReadError: The operation did not complete (read)


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:

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)

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 ssl_socket.send(), so I'm not sure what I am missing.

Note that I have a perl client that works properly as follows for context:

#!/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 => 'hostname',
    PeerPort => '12345',
    Proto => 'tcp',
    SSL_cert_file => $ENV{'HOME'} . '/path/to/client.pem',
    SSL_key_file => $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 = ( <$socket> ) ;
print "@r";

$socket->close();

With output:

connected to the server
{
   "password": "passwd",
   "username": "username"
}

What is the proper way to use the Python SSL library to retrieve my requested data?


Solution

  • Answer: needed a '\n' character at the end of my data string.