diff options
Diffstat (limited to 'socket_conn.py')
-rwxr-xr-x | socket_conn.py | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/socket_conn.py b/socket_conn.py index 433cc75..4367240 100755 --- a/socket_conn.py +++ b/socket_conn.py @@ -6,6 +6,7 @@ import socket
from zope import interface
import errno
+import select
from interfaces import endpoint
@@ -25,15 +26,22 @@ class SocketConnection(object): """ Connect """
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s.connect((self.host, self.port))
- self.s.settimeout(0.0)
component.handle(Debug("Connected to the socket"))
def read(self) -> str:
""" Read from the connection and return the bytes.
Return None if there is nothing to read (non-blocking)
Raise an exception if disconnected """
+ # check the socket for reading in non-blocking mode
+ read_socket, _, _ = select.select([self.s], [], [], 0.0)
+ if read_socket == []: return
try:
- return self.s.recv(1024)
+ recv = self.s.recv(1024)
+ if recv == b"":
+ # We should be able to read something, but got nothing.
+ # Reset the socket
+ raise RuntimeError("socket connection broken")
+ return recv
except socket.error as e:
err = e.args[0]
if err == errno.EAGAIN or err == errno.EWOULDBLOCK:
|