本文整理汇总了Python中twisted.protocols.basic.LineReceiver方法的典型用法代码示例。如果您正苦于以下问题:Python basic.LineReceiver方法的具体用法?Python basic.LineReceiver怎么用?Python basic.LineReceiver使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.protocols.basic
的用法示例。
在下文中一共展示了basic.LineReceiver方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: dataReceived
# 需要导入模块: from twisted.protocols import basic [as 别名]
# 或者: from twisted.protocols.basic import LineReceiver [as 别名]
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)
示例2: test_clearLineBuffer
# 需要导入模块: from twisted.protocols import basic [as 别名]
# 或者: from twisted.protocols.basic import LineReceiver [as 别名]
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'')
示例3: rawDataReceived
# 需要导入模块: from twisted.protocols import basic [as 别名]
# 或者: from twisted.protocols.basic import LineReceiver [as 别名]
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)
示例4: dataReceived
# 需要导入模块: from twisted.protocols import basic [as 别名]
# 或者: from twisted.protocols.basic import LineReceiver [as 别名]
def dataReceived(self, data):
"""
Data was received from the network. Process it.
"""
# If we're currently handling a request, buffer this data.
if self._handlingRequest:
self._dataBuffer.append(data)
if (
(sum(map(len, self._dataBuffer)) >
self._optimisticEagerReadSize)
and not self._waitingForTransport
):
# If we received more data than a small limit while processing
# the head-of-line request, apply TCP backpressure to our peer
# to get them to stop sending more request data until we're
# ready. See docstring for _optimisticEagerReadSize above.
self._networkProducer.pauseProducing()
return
return basic.LineReceiver.dataReceived(self, data)
示例5: test_notQuiteMaximumLineLengthUnfinished
# 需要导入模块: from twisted.protocols import basic [as 别名]
# 或者: from twisted.protocols.basic import LineReceiver [as 别名]
def test_notQuiteMaximumLineLengthUnfinished(self):
"""
C{LineReceiver} doesn't disconnect the transport it if
receives a non-finished line whose length, counting the
delimiter, is longer than its C{MAX_LENGTH} but shorter than
its C{MAX_LENGTH} + len(delimiter). (When the first part that
exceeds the max is the beginning of the delimiter.)
"""
proto = basic.LineReceiver()
# '\r\n' is the default, but we set it just to be explicit in
# this test.
proto.delimiter = b'\r\n'
transport = proto_helpers.StringTransport()
proto.makeConnection(transport)
proto.dataReceived((b'x' * proto.MAX_LENGTH)
+ proto.delimiter[:len(proto.delimiter)-1])
self.assertFalse(transport.disconnecting)
示例6: dataReceived
# 需要导入模块: from twisted.protocols import basic [as 别名]
# 或者: from twisted.protocols.basic import LineReceiver [as 别名]
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)
示例7: test_clearLineBuffer
# 需要导入模块: from twisted.protocols import basic [as 别名]
# 或者: from twisted.protocols.basic import LineReceiver [as 别名]
def test_clearLineBuffer(self):
"""
L{LineReceiver.clearLineBuffer} removes all buffered data and returns
it as a C{str} 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('foo\r\nbar\r\nbaz')
self.assertEqual(protocol.line, 'foo')
self.assertEqual(protocol.rest, 'bar\r\nbaz')
# Deliver another line to make sure the previously buffered data is
# really gone.
protocol.dataReceived('quux\r\n')
self.assertEqual(protocol.line, 'quux')
self.assertEqual(protocol.rest, '')
示例8: dataReceived
# 需要导入模块: from twisted.protocols import basic [as 别名]
# 或者: from twisted.protocols.basic import LineReceiver [as 别名]
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)
示例9: _reallySendLine
# 需要导入模块: from twisted.protocols import basic [as 别名]
# 或者: from twisted.protocols.basic import LineReceiver [as 别名]
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)
示例10: connectionLost
# 需要导入模块: from twisted.protocols import basic [as 别名]
# 或者: from twisted.protocols.basic import LineReceiver [as 别名]
def connectionLost(self, reason):
basic.LineReceiver.connectionLost(self, reason)
self.stopHeartbeat()
示例11: dataReceived
# 需要导入模块: from twisted.protocols import basic [as 别名]
# 或者: from twisted.protocols.basic import LineReceiver [as 别名]
def dataReceived(self, data):
try:
if isinstance(data, unicode):
data = data.encode("utf-8")
basic.LineReceiver.dataReceived(self, data)
except:
log.err()
self.invalidMessage()
示例12: sendLine
# 需要导入模块: from twisted.protocols import basic [as 别名]
# 或者: from twisted.protocols.basic import LineReceiver [as 别名]
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)
示例13: test_rawDataError
# 需要导入模块: from twisted.protocols import basic [as 别名]
# 或者: from twisted.protocols.basic import LineReceiver [as 别名]
def test_rawDataError(self):
"""
C{LineReceiver.dataReceived} forwards errors returned by
C{rawDataReceived}.
"""
proto = basic.LineReceiver()
proto.rawDataReceived = lambda data: RuntimeError("oops")
transport = proto_helpers.StringTransport()
proto.makeConnection(transport)
proto.setRawMode()
why = proto.dataReceived(b'data')
self.assertIsInstance(why, RuntimeError)
示例14: test_rawDataReceivedNotImplemented
# 需要导入模块: from twisted.protocols import basic [as 别名]
# 或者: from twisted.protocols.basic import LineReceiver [as 别名]
def test_rawDataReceivedNotImplemented(self):
"""
When L{LineReceiver.rawDataReceived} is not overridden in a
subclass, calling it raises C{NotImplementedError}.
"""
proto = basic.LineReceiver()
self.assertRaises(NotImplementedError, proto.rawDataReceived, 'foo')
示例15: test_longUnendedLine
# 需要导入模块: from twisted.protocols import basic [as 别名]
# 或者: from twisted.protocols.basic import LineReceiver [as 别名]
def test_longUnendedLine(self):
"""
If more bytes than C{LineReceiver.MAX_LENGTH} arrive containing no line
delimiter, all of the bytes are passed as a single string to
L{LineReceiver.lineLengthExceeded}.
"""
excessive = b'x' * (self.proto.MAX_LENGTH * 2 + 2)
self.proto.dataReceived(excessive)
self.assertEqual([excessive], self.proto.longLines)