I'm seeing a weird issue when starting a TLS connection to any host. If I don't set any timeout on the socket, it works fine. If I do, it breaks before the timeout with a OpenSSL.SSL.WantReadError
. For example if I set timeout to 100, it will break after a second anyway.
For now I use a workaround of setting a timeout on connection, but then removing it before the handshake. How can I fix this to respect the timeout instead?
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
ctx = OpenSSL.SSL.Context(OpenSSL.SSL.TLSv1_METHOD)
ctx.set_options(OpenSSL.SSL.OP_NO_SSLv2 | OpenSSL.SSL.OP_NO_SSLv3)
ctx.set_verify(OpenSSL.SSL.VERIFY_NONE, lambda _a, _b, _c, _d, _e: None)
conn = OpenSSL.SSL.Connection(ctx, s)
conn.set_tlsext_host_name(hostname.encode('utf-8'))
conn.connect((ip, port))
s.settimeout(None)
try:
conn.do_handshake()
except OpenSSL.SSL.WantReadError:
# this happens on every connection