def read(self):
'''Read a line of data from the server, if any.'''
# Only do something if we're connected.
if self.__connected:
done = False
received = ""
while not done:
try:
if self.ssl:
data = self.__ssl.recv(1)
else:
data = self.__socket.recv(1)
except (ssl.SSLWantReadError, BlockingIOError):
received = None
break
except OSError as err:
debug.error("Error #" + str(err.errno) + ": '" + err.strerror + "' disconnecting.")
data = False
# Process the data.
# socket.recv is supposed to return a False if the connection
# been broken.
if not data:
self.disconnect()
done = True
received = None
else:
text = data.decode('utf-8','replace')
if text == '\n':
done = True
else:
received += text
else:
received = None
# Remove the trailing carriage return character (cr/lf pair)
if not received is None:
received = received.strip('\r')
if len(received) > 0:
if received[0] == ':':
received = received[1:]
# Bug fix for Issue #18, do not return blank lines.
if received == "":
received = None
return received
评论列表
文章目录