How to timeout a recv() on an ssl connection?

G

Grant Edwards

I can't figure out how to timeout a recv() on an SSLSocket -- I'm using
Python 2.6 nad 2.7. Here's what I've got so far (it needs to work on
either a plain or SSL socket):

s.settimeout(timeout)
try:
b = ord(s.recv(1))
except socket.timeout:
return None
except ssl.SSLError as e:
if e.message == 'The read operation timed out':
return None
else:
raise
finally:
s.settimeout(None)


It works fine on normal sockets. With an SSLSocket, it works the
first time it's called with timeout set to something other than None
(it times out properly). Subsequence reads with timeout set to None
work, but then the second time it's called with a non-None timeout, it
hangs for several minutes, and then the recv() returns an empty
string.
 
G

Grant Edwards

I can't figure out how to timeout a recv() on an SSLSocket -- I'm using
Python 2.6 nad 2.7. Here's what I've got so far (it needs to work on
either a plain or SSL socket):

s.settimeout(timeout)
try:
b = ord(s.recv(1))
except socket.timeout:
return None
except ssl.SSLError as e:
if e.message == 'The read operation timed out':
return None
else:
raise
finally:
s.settimeout(None)


It works fine on normal sockets. With an SSLSocket, it works the
first time it's called with timeout set to something other than None
(it times out properly). Subsequence reads with timeout set to None
work, but then the second time it's called with a non-None timeout, it
hangs for several minutes, and then the recv() returns an empty
string.

Doh! Never mind.

The above code does appear to be working correctly. I had a different
bug that was misleading me...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,434
Messages
2,571,689
Members
48,796
Latest member
Greg L.

Latest Threads

Top