def dataReceived(self, data):
"""This hack is to support mIRC, which sends LF only,
even though the RFC says CRLF. (Also, the flexibility
of LineReceiver to turn "line mode" on and off was not
required.)
"""
lines = (self.buffer + data).split(LF)
# Put the (possibly empty) element after the last LF back in the
# buffer
self.buffer = lines.pop()
for line in lines:
if len(line) <= 2:
# This is a blank line, at best.
continue
if line[-1] == CR:
line = line[:-1]
prefix, command, params = parsemsg(line)
# mIRC is a big pile of doo-doo
command = command.upper()
# DEBUG: log.msg( "%s %s %s" % (prefix, command, params))
self.handleCommand(command, prefix, params)
python类LineReceiver()的实例源码
def rawDataReceived(self, data):
if self.timeout > 0:
self.resetTimeout()
self._pendingSize -= len(data)
if self._pendingSize > 0:
self._pendingBuffer.write(data)
else:
passon = ''
if self._pendingSize < 0:
data, passon = data[:self._pendingSize], data[self._pendingSize:]
self._pendingBuffer.write(data)
rest = self._pendingBuffer
self._pendingBuffer = None
self._pendingSize = None
rest.seek(0, 0)
self._parts.append(rest.read())
self.setLineMode(passon.lstrip('\r\n'))
# def sendLine(self, line):
# print 'S:', repr(line)
# return basic.LineReceiver.sendLine(self, line)
def dataReceived(self, data):
"""This hack is to support mIRC, which sends LF only,
even though the RFC says CRLF. (Also, the flexibility
of LineReceiver to turn "line mode" on and off was not
required.)
"""
lines = (self.buffer + data).split(LF)
# Put the (possibly empty) element after the last LF back in the
# buffer
self.buffer = lines.pop()
for line in lines:
if len(line) <= 2:
# This is a blank line, at best.
continue
if line[-1] == CR:
line = line[:-1]
prefix, command, params = parsemsg(line)
# mIRC is a big pile of doo-doo
command = command.upper()
# DEBUG: log.msg( "%s %s %s" % (prefix, command, params))
self.handleCommand(command, prefix, params)
def rawDataReceived(self, data):
if self.timeout > 0:
self.resetTimeout()
self._pendingSize -= len(data)
if self._pendingSize > 0:
self._pendingBuffer.write(data)
else:
passon = ''
if self._pendingSize < 0:
data, passon = data[:self._pendingSize], data[self._pendingSize:]
self._pendingBuffer.write(data)
rest = self._pendingBuffer
self._pendingBuffer = None
self._pendingSize = None
rest.seek(0, 0)
self._parts.append(rest.read())
self.setLineMode(passon.lstrip('\r\n'))
# def sendLine(self, line):
# print 'S:', repr(line)
# return basic.LineReceiver.sendLine(self, line)
def dataReceived(self, data):
"""
This hack is to support mIRC, which sends LF only, even though the RFC
says CRLF. (Also, the flexibility of LineReceiver to turn "line mode"
on and off was not required.)
"""
if isinstance(data, bytes):
data = data.decode("utf-8")
lines = (self.buffer + data).split(LF)
# Put the (possibly empty) element after the last LF back in the
# buffer
self.buffer = lines.pop()
for line in lines:
if len(line) <= 2:
# This is a blank line, at best.
continue
if line[-1] == CR:
line = line[:-1]
prefix, command, params = parsemsg(line)
# mIRC is a big pile of doo-doo
command = command.upper()
# DEBUG: log.msg( "%s %s %s" % (prefix, command, params))
self.handleCommand(command, prefix, params)
def test_clearLineBuffer(self):
"""
L{LineReceiver.clearLineBuffer} removes all buffered data and returns
it as a C{bytes} and can be called from beneath C{dataReceived}.
"""
class ClearingReceiver(basic.LineReceiver):
def lineReceived(self, line):
self.line = line
self.rest = self.clearLineBuffer()
protocol = ClearingReceiver()
protocol.dataReceived(b'foo\r\nbar\r\nbaz')
self.assertEqual(protocol.line, b'foo')
self.assertEqual(protocol.rest, b'bar\r\nbaz')
# Deliver another line to make sure the previously buffered data is
# really gone.
protocol.dataReceived(b'quux\r\n')
self.assertEqual(protocol.line, b'quux')
self.assertEqual(protocol.rest, b'')
def rawDataReceived(self, data):
if self.timeout > 0:
self.resetTimeout()
self._pendingSize -= len(data)
if self._pendingSize > 0:
self._pendingBuffer.write(data)
else:
passon = ''
if self._pendingSize < 0:
data, passon = data[:self._pendingSize], data[self._pendingSize:]
self._pendingBuffer.write(data)
rest = self._pendingBuffer
self._pendingBuffer = None
self._pendingSize = None
rest.seek(0, 0)
self._parts.append(rest.read())
self.setLineMode(passon.lstrip('\r\n'))
# def sendLine(self, line):
# print 'S:', repr(line)
# return basic.LineReceiver.sendLine(self, line)
def dataReceived(self, data):
"""This hack is to support mIRC, which sends LF only,
even though the RFC says CRLF. (Also, the flexibility
of LineReceiver to turn "line mode" on and off was not
required.)
"""
lines = (self.buffer + data).split(LF)
# Put the (possibly empty) element after the last LF back in the
# buffer
self.buffer = lines.pop()
for line in lines:
if len(line) <= 2:
# This is a blank line, at best.
continue
if line[-1] == CR:
line = line[:-1]
prefix, command, params = parsemsg(line)
# mIRC is a big pile of doo-doo
command = command.upper()
# DEBUG: log.msg( "%s %s %s" % (prefix, command, params))
self.handleCommand(command, prefix, params)
def connectionMade(self):
basic.LineReceiver.connectionMade(self)
self.expectingSalt = True
def _reallySendLine(self, line):
return basic.LineReceiver.sendLine(self, lowQuote(line) + '\r')
def dataReceived(self, data):
basic.LineReceiver.dataReceived(self, data.replace('\r', ''))
def connectionLost(self, reason):
basic.LineReceiver.connectionLost(self, reason)
TelnetProtocol.connectionLost(self, reason)
def sendLine(self, line):
"""Throw up if the line is longer than 1022 characters"""
if len(line) > self.MAX_LENGTH - 2:
raise ValueError("DictClient tried to send a too long line")
basic.LineReceiver.sendLine(self, line)
def sendLine(self, line):
"""(Private) Sends a line, unless line is None."""
if line is None:
return
basic.LineReceiver.sendLine(self, line)
def dataReceived(self, data):
try:
basic.LineReceiver.dataReceived(self, data)
except:
log.err()
self.invalidMessage()
def _unblock(self):
commands = self.blocked
self.blocked = None
while commands and self.blocked is None:
self.lineReceived(commands.pop(0))
if self.blocked is not None:
self.blocked.extend(commands)
# def sendLine(self, line):
# print 'C:', repr(line)
# return basic.LineReceiver.sendLine(self, line)
def sendLine(self, line):
# Log sendLine only if you are in debug mode for performance
if self.debug:
self.log.append('>>> ' + line)
basic.LineReceiver.sendLine(self,line)
def _reallySendLine(self, line):
return basic.LineReceiver.sendLine(self, lowQuote(line) + '\r')
def dataReceived(self, data):
basic.LineReceiver.dataReceived(self, data.replace('\r', ''))
def connectionLost(self, reason):
basic.LineReceiver.connectionLost(self, reason)
TelnetProtocol.connectionLost(self, reason)
def sendLine(self, line):
"""Throw up if the line is longer than 1022 characters"""
if len(line) > self.MAX_LENGTH - 2:
raise ValueError("DictClient tried to send a too long line")
basic.LineReceiver.sendLine(self, line)
def sendLine(self, line):
"""(Private) Sends a line, unless line is None."""
if line is None:
return
basic.LineReceiver.sendLine(self, line)
def dataReceived(self, data):
try:
basic.LineReceiver.dataReceived(self, data)
except:
log.err()
self.invalidMessage()
def _unblock(self):
commands = self.blocked
self.blocked = None
while commands and self.blocked is None:
self.lineReceived(commands.pop(0))
if self.blocked is not None:
self.blocked.extend(commands)
# def sendLine(self, line):
# print 'C:', repr(line)
# return basic.LineReceiver.sendLine(self, line)
def sendLine(self, line):
# Log sendLine only if you are in debug mode for performance
if self.debug:
self.log.append('>>> ' + line)
basic.LineReceiver.sendLine(self,line)
def _reallySendLine(self, line):
quoteLine = lowQuote(line)
if isinstance(quoteLine, unicode):
quoteLine = quoteLine.encode("utf-8")
quoteLine += b'\r'
return basic.LineReceiver.sendLine(self, quoteLine)
def connectionLost(self, reason):
basic.LineReceiver.connectionLost(self, reason)
self.stopHeartbeat()
def dataReceived(self, data):
if isinstance(data, unicode):
data = data.encode("utf-8")
data = data.replace(b'\r', b'')
basic.LineReceiver.dataReceived(self, data)
def connectionLost(self, reason):
basic.LineReceiver.connectionLost(self, reason)
TelnetProtocol.connectionLost(self, reason)
def sendLine(self, line):
"""
Sends a line, unless line is None.
@param line: Line to send
@type line: L{bytes} or L{unicode}
"""
if line is None:
return
elif isinstance(line, unicode):
line = line.encode(self._encoding)
basic.LineReceiver.sendLine(self, line)