本文整理汇总了Python中twisted.protocols.basic.LineReceiver.dataReceived方法的典型用法代码示例。如果您正苦于以下问题:Python LineReceiver.dataReceived方法的具体用法?Python LineReceiver.dataReceived怎么用?Python LineReceiver.dataReceived使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.protocols.basic.LineReceiver
的用法示例。
在下文中一共展示了LineReceiver.dataReceived方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: dataReceived
# 需要导入模块: from twisted.protocols.basic import LineReceiver [as 别名]
# 或者: from twisted.protocols.basic.LineReceiver import dataReceived [as 别名]
def dataReceived(self, *args, **kwargs):
# receives the data then checks if the request is complete.
# if it is, it calls full_Request_received
LineReceiver.dataReceived(self, *args, **kwargs)
if self._request_obj.complete:
self.full_request_received()
示例2: APRSProcessProtocol
# 需要导入模块: from twisted.protocols.basic import LineReceiver [as 别名]
# 或者: from twisted.protocols.basic.LineReceiver import dataReceived [as 别名]
class APRSProcessProtocol(ProcessProtocol):
def __init__(self, target):
self.__target = target
self.__line_receiver = LineReceiver()
self.__line_receiver.delimiter = '\n'
self.__line_receiver.lineReceived = self.__lineReceived
self.__last_line = None
def outReceived(self, data):
# split lines
self.__line_receiver.dataReceived(data)
def errReceived(self, data):
# we should inherit stderr, not pipe it
raise Exception('shouldn\'t happen')
def __lineReceived(self, line):
if line == '': # observed glitch in output
pass
elif line.startswith('Enabled demodulators:'):
pass
elif line.startswith('$ULTW') and self.__last_line is not None: # observed glitch in output; need to glue to previous line, I think?
ll = self.__last_line
self.__last_line = None
self.__target(ll + line)
elif line.startswith('APRS: '):
line = line[len('APRS: '):]
self.__last_line = line
self.__target(line)
else:
# TODO: Log these properly
print 'Not APRS line: %r' % line
示例3: RTL433ProcessProtocol
# 需要导入模块: from twisted.protocols.basic import LineReceiver [as 别名]
# 或者: from twisted.protocols.basic.LineReceiver import dataReceived [as 别名]
class RTL433ProcessProtocol(ProcessProtocol):
def __init__(self, target):
self.__target = target
self.__line_receiver = LineReceiver()
self.__line_receiver.delimiter = '\n'
self.__line_receiver.lineReceived = self.__lineReceived
def outReceived(self, data):
"""Implements ProcessProtocol."""
# split lines
self.__line_receiver.dataReceived(data)
def errReceived(self, data):
"""Implements ProcessProtocol."""
# we should inherit stderr, not pipe it
raise Exception('shouldn\'t happen')
def __lineReceived(self, line):
# rtl_433's JSON encoder is not perfect (e.g. it will emit unescaped newlines), so protect against parse failures
try:
message = json.loads(line)
except ValueError:
log.msg('bad JSON from rtl_433: %s' % line)
return
log.msg('rtl_433 message: %r' % (message,))
# rtl_433 provides a time field, but when in file-input mode it assumes the input is not real-time and generates start-of-file-relative timestamps, so we can't use them.
wrapper = RTL433MessageWrapper(message, time.time())
self.__target(wrapper)
示例4: dataReceived
# 需要导入模块: from twisted.protocols.basic import LineReceiver [as 别名]
# 或者: from twisted.protocols.basic.LineReceiver import dataReceived [as 别名]
def dataReceived(self, data):
if self.factory.stream_response and self.stream_response:
self.factory.return_transport.write(data)
LineReceiver.dataReceived(self, data)
if not self.completed:
if self._response_obj.complete:
self.completed = True
self.handle_response_end()
示例5: PostgresMonitor
# 需要导入模块: from twisted.protocols.basic import LineReceiver [as 别名]
# 或者: from twisted.protocols.basic.LineReceiver import dataReceived [as 别名]
class PostgresMonitor(ProcessProtocol):
"""
A monitoring protocol which watches the postgres subprocess.
"""
log = Logger()
def __init__(self, svc=None):
self.lineReceiver = LineReceiver()
self.lineReceiver.delimiter = '\n'
self.lineReceiver.lineReceived = self.lineReceived
self.svc = svc
self.isReady = False
self.completionDeferred = Deferred()
def lineReceived(self, line):
if self.svc is None:
return
if not self.isReady:
if _MAGIC_READY_COOKIE in line:
self.svc.ready()
disconnecting = False
def connectionMade(self):
self.lineReceiver.makeConnection(self)
def outReceived(self, out):
for line in out.split("\n"):
if line:
self.log.info("{message}", message=line)
# self.lineReceiver.dataReceived(out)
def errReceived(self, err):
for line in err.split("\n"):
if line:
self.log.error("{message}", message=line)
self.lineReceiver.dataReceived(err)
def processEnded(self, reason):
self.log.info(
"pg_ctl process ended with status={status}",
status=reason.value.status
)
# If pg_ctl exited with zero, we were successful in starting postgres
# If pg_ctl exited with nonzero, we need to give up.
self.lineReceiver.connectionLost(reason)
if reason.value.status == 0:
self.completionDeferred.callback(None)
else:
self.log.error("Could not start postgres; see postgres.log")
self.completionDeferred.errback(reason)
示例6: dataReceived
# 需要导入模块: from twisted.protocols.basic import LineReceiver [as 别名]
# 或者: from twisted.protocols.basic.LineReceiver import dataReceived [as 别名]
def dataReceived(self, *args, **kwargs):
# receives the data then checks if the request is complete.
# if it is, it calls full_Request_received
LineReceiver.dataReceived(self, *args, **kwargs)
if self._request_obj.complete:
try:
self.full_request_received()
except PappyException as e:
print str(e)
示例7: CommandReceiver
# 需要导入模块: from twisted.protocols.basic import LineReceiver [as 别名]
# 或者: from twisted.protocols.basic.LineReceiver import dataReceived [as 别名]
class CommandReceiver(channel.ChannelReceiver):
def __init__(self):
self.lineReceiver = LineReceiver()
self.lineReceiver.lineReceived = self.lineReceived
def lineReceived(self, line):
cmd, data = line.split(':', 1)
self.commandReceived(cmd.strip(), data.strip())
def commandReceived(self, cmd, data):
raise NotImplementedError
def sendCommand(self, cmd, data):
return self.send(self.cmd_channel, '%s: %s\r\n' % (cmd, data))
def channeReceived(self, channel, type, data):
if channel == self.cmd_channel:
self.lineReceiver.dataReceived(data)
示例8: _PostgresMonitor
# 需要导入模块: from twisted.protocols.basic import LineReceiver [as 别名]
# 或者: from twisted.protocols.basic.LineReceiver import dataReceived [as 别名]
class _PostgresMonitor(ProcessProtocol):
"""
A monitoring protocol which watches the postgres subprocess.
"""
def __init__(self, svc=None):
self.lineReceiver = LineReceiver()
self.lineReceiver.delimiter = '\n'
self.lineReceiver.lineReceived = self.lineReceived
self.svc = svc
self.isReady = False
self.completionDeferred = Deferred()
def lineReceived(self, line):
if self.svc is None:
return
if not self.isReady:
if _MAGIC_READY_COOKIE in line:
self.svc.ready()
disconnecting = False
def connectionMade(self):
self.lineReceiver.makeConnection(self)
def outReceived(self, out):
log.msg("received postgres stdout %r" % (out,))
# self.lineReceiver.dataReceived(out)
def errReceived(self, err):
log.msg("received postgres stderr %r" % (err,))
self.lineReceiver.dataReceived(err)
def processEnded(self, reason):
log.msg("postgres process ended %r" % (reason,))
result = (reason.value.status == 0)
self.lineReceiver.connectionLost(reason)
self.completionDeferred.callback(result)
示例9: LineBuffer
# 需要导入模块: from twisted.protocols.basic import LineReceiver [as 别名]
# 或者: from twisted.protocols.basic.LineReceiver import dataReceived [as 别名]
class LineBuffer(object):
"""
A line-buffering wrapper for L{GreenletTransport}s (or any other object
with C{read} and C{write} methods). Call L{readLine} to get the next line,
call L{writeLine} to write a line.
"""
def __init__(self, transport, delimiter="\r\n"):
"""
@param transport: The transport from which to read bytes and to which
to write them!
@type transport: L{GreenletTransport}
@param delimiter: The line delimiter to split lines on.
@type delimiter: C{str}
"""
self.delimiter = delimiter
self.transport = transport
self.receiver = LineReceiver()
self.receiver.delimiter = delimiter
self.lines = []
self.receiver.lineReceived = self.lines.append
def writeLine(self, data):
"""
Write some data to the transport followed by the delimiter.
"""
self.transport.write(data + self.delimiter)
def readLine(self):
"""
Return a line of data from the transport.
"""
while not self.lines:
self.receiver.dataReceived(self.transport.read())
return self.lines.pop(0)
def __iter__(self):
"""
Yield the result of L{readLine} forever.
"""
while True:
yield self.readLine()
示例10: _ControllerProtocol
# 需要导入模块: from twisted.protocols.basic import LineReceiver [as 别名]
# 或者: from twisted.protocols.basic.LineReceiver import dataReceived [as 别名]
class _ControllerProtocol(Protocol):
def __init__(self):
self.__line_receiver = LineReceiver()
self.__line_receiver.delimiter = b';'
self.__line_receiver.lineReceived = self.__lineReceived
def connectionMade(self):
"""overrides Protocol"""
# TODO: Report success
def connectionLost(self, reason=None):
"""overrides Protocol"""
# TODO: Report loss to user
def dataReceived(self, data):
"""overrides Protocol"""
self.__line_receiver.dataReceived(data)
def __lineReceived(self, line):
print line
def send(self, cmd):
self.transport.write(cmd)
示例11: dataReceived
# 需要导入模块: from twisted.protocols.basic import LineReceiver [as 别名]
# 或者: from twisted.protocols.basic.LineReceiver import dataReceived [as 别名]
def dataReceived(self, data):
# translate '\r' into '\n'
return LineReceiver.dataReceived(self, data.replace('\r', '\n'))
示例12: dataReceived
# 需要导入模块: from twisted.protocols.basic import LineReceiver [as 别名]
# 或者: from twisted.protocols.basic.LineReceiver import dataReceived [as 别名]
def dataReceived(self, data):
self.resetTimeout()
LineReceiver.dataReceived(self, data)
示例13: dataReceived
# 需要导入模块: from twisted.protocols.basic import LineReceiver [as 别名]
# 或者: from twisted.protocols.basic.LineReceiver import dataReceived [as 别名]
def dataReceived(self, data):
LineReceiver.dataReceived(self, data)
if data == "\x04":
self.transport.loseConnection()
示例14: _HamlibClientProtocol
# 需要导入模块: from twisted.protocols.basic import LineReceiver [as 别名]
# 或者: from twisted.protocols.basic.LineReceiver import dataReceived [as 别名]
class _HamlibClientProtocol(Protocol):
def __init__(self, server_name, connected_deferred):
self.__proxy_obj = None
self.__server_name = server_name
self.__connected_deferred = connected_deferred
self.__line_receiver = LineReceiver()
self.__line_receiver.delimiter = '\n'
self.__line_receiver.lineReceived = self.__lineReceived
self.__waiting_for_responses = []
self.__receive_cmd = None
self.__receive_arg = None
def connectionMade(self):
self.__connected_deferred.callback(self)
def connectionLost(self, reason):
# pylint: disable=signature-differs
if self.__proxy_obj is not None:
self.__proxy_obj._clientConnectionLost(reason)
def dataReceived(self, data):
self.__line_receiver.dataReceived(data)
def __lineReceived(self, line):
if self.__receive_cmd is None:
match = re.match(r'^(\w+):\s*(.*)$', line)
if match is not None:
# command response starting line
self.__receive_cmd = match.group(1)
self.__receive_arg = match.group(2)
return
log.err('%s client: Unrecognized line (no command active): %r' % (self.__server_name, line))
else:
match = re.match(r'^RPRT (-?\d+)$', line)
if match is not None:
# command response ending line
return_code = int(match.group(1))
waiting = self.__waiting_for_responses
i = 0
for i, (wait_cmd, wait_deferred) in enumerate(waiting):
if self.__receive_cmd != wait_cmd:
log.err("%s client: Didn't get a response for command %r before receiving one for command %r" % (self.__server_name, wait_cmd, self.__receive_cmd))
else:
# TODO: Consider 'parsing' return code more here.
if return_code != 0:
self.__proxy_obj._clientError(self.__receive_cmd, return_code)
wait_deferred.callback(return_code)
break
self.__waiting_for_responses = waiting[i + 1:]
self.__receive_cmd = None
self.__receive_arg = None
return
if self.__receive_cmd == 'get_level':
# Should be a level value
match = re.match(r'^-?\d+\.?\d*$', line)
if match:
self.__proxy_obj._clientReceivedLevel(self.__receive_arg, line)
return
match = re.match(r'^([\w ,/-]+):\s*(.*)$', line)
if match is not None:
# Command response
if self.__proxy_obj is not None:
self.__proxy_obj._clientReceived(self.__receive_cmd, match.group(1), match.group(2))
return
match = re.match(r'^\t', line)
if match is not None and self.__receive_cmd == 'dump_caps':
# Sub-info from dump_caps, not currently used
return
match = re.match(r'^Warning--', line)
if match is not None:
# Warning from dump_caps, not currently used
return
match = re.match(r'^$', line)
if match is not None:
return
log.err('%s client: Unrecognized line during %s: %r' % (self.__server_name, self.__receive_cmd, line))
def _set_proxy(self, proxy):
self.__proxy_obj = proxy
def rc_send(self, cmd, argstr=''):
if not re.match(r'^\w+$', cmd): # no spaces (stuffing args in), no newlines (breaking the command)
raise ValueError('Syntactically invalid command name %r' % (cmd,))
if not re.match(r'^[^\r\n]*$', argstr): # no newlines
raise ValueError('Syntactically invalid arguments string %r' % (cmd,))
self.transport.write('+\\' + cmd + ' ' + argstr + '\n')
d = defer.Deferred()
self.__waiting_for_responses.append((cmd, d))
return d
示例15: dataReceived
# 需要导入模块: from twisted.protocols.basic import LineReceiver [as 别名]
# 或者: from twisted.protocols.basic.LineReceiver import dataReceived [as 别名]
def dataReceived(self, data):
if data == "\r":
data = "\r\n"
return LineReceiver.dataReceived(self, data)