Skip to content

Commit 938f80d

Browse files
Update recipe-408859.py
Detection for closed/broken sockets in recv_timeout.
1 parent 2ed807a commit 938f80d

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

recipes/Python/408859_socketrecv__three_ways_turn_it/recipe-408859.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ def recv_timeout(the_socket,timeout=2):
2727
total_data.append(data)
2828
begin=time.time()
2929
else:
30-
time.sleep(0.1)
30+
# Incase there is actually no data to be read from a non blocking socket,
31+
# a socket.error exception is raised with EWOULDBLOCK OR EAGAIN
32+
# If there is data of zero length read, it implies the socket was closed on the other end.
33+
the_socket.close()
34+
break
3135
except:
3236
pass
3337
return ''.join(total_data)

0 commit comments

Comments
 (0)