本文整理匯總了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)