本文整理汇总了Python中twisted.protocols.policies.ProtocolWrapper类的典型用法代码示例。如果您正苦于以下问题:Python ProtocolWrapper类的具体用法?Python ProtocolWrapper怎么用?Python ProtocolWrapper使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ProtocolWrapper类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: makeConnection
def makeConnection(self, transport):
"""
Connect this wrapper to the given transport and initialize the
necessary L{OpenSSL.SSL.Connection} with a memory BIO.
"""
self._tlsConnection = self.factory._createConnection(self)
self._appSendBuffer = []
# Add interfaces provided by the transport we are wrapping:
for interface in providedBy(transport):
directlyProvides(self, interface)
# Intentionally skip ProtocolWrapper.makeConnection - it might call
# wrappedProtocol.makeConnection, which we want to make conditional.
Protocol.makeConnection(self, transport)
self.factory.registerProtocol(self)
if self._connectWrapped:
# Now that the TLS layer is initialized, notify the application of
# the connection.
ProtocolWrapper.makeConnection(self, transport)
# Now that we ourselves have a transport (initialized by the
# ProtocolWrapper.makeConnection call above), kick off the TLS
# handshake.
self._checkHandshakeStatus()
示例2: parseFrames
def parseFrames(self):
"""
Find frames in incoming data and pass them to the underlying protocol.
"""
if self.flavor == HYBI00:
parser = parse_hybi00_frames
elif self.flavor in (HYBI07, HYBI10, RFC6455):
parser = parse_hybi07_frames
else:
raise WSException("Unknown flavor %r" % self.flavor)
try:
frames, self.buf = parser(self.buf)
except WSException as wse:
# Couldn't parse all the frames, something went wrong, let's bail.
self.close(wse.args[0])
return
for frame in frames:
opcode, data = frame
if opcode == NORMAL:
# Business as usual. Decode the frame, if we have a decoder.
if self.codec:
data = decoders[self.codec](data)
# Pass the frame to the underlying protocol.
ProtocolWrapper.dataReceived(self, data)
elif opcode == CLOSE:
# The other side wants us to close. I wonder why?
reason, text = data
log.msg("Closing connection: %r (%d)" % (text, reason))
# Close the connection.
self.close()
示例3: __init__
def __init__(self, factory, wrappedProtocol):
ProtocolWrapper.__init__(self, factory, wrappedProtocol)
AsyncStateMachine.__init__(self)
self.fakeSocket = _FakeSocket(self)
self.tlsConnection = TLSConnection(self.fakeSocket)
self.tlsStarted = False
self.connectionLostCalled = False
示例4: writeSequence
def writeSequence(self, data):
# type: (List[bytes]) -> None
if not self.tlsStarted:
ProtocolWrapper.writeSequence(self, b''.join(data))
return
self.write(b''.join(data))
示例5: __init__
def __init__(self, factory, wrappedProtocol, connectedDeferred, host, port):
ProtocolWrapper.__init__(self, factory, wrappedProtocol)
self._connectedDeferred = connectedDeferred
self._host = host
self._port = port
self._buf = b''
self.state = 0
示例6: dataReceived
def dataReceived(self, data):
"""
Append the data to the buffer list and parse the whole.
"""
self._buffer.append(data)
if self.challenge:
buf = "".join(self._buffer)
if len(buf) >= 8:
challenge, buf = buf[:8], buf[8:]
self._buffer = [buf]
nonce = self.challenge(challenge)
self.transport.write(nonce)
self.challenge = None
if self.connected:
ProtocolWrapper.connectionMade(self)
self.dataReceived("") # Kick it off proper
if self.pending_dc:
self.pending_dc = False
self.loseConnection()
else:
self._parseFrames()
if self._pending_frames:
self._sendFrames()
self._parseFrames()
示例7: makeConnection
def makeConnection(self, transport):
"""
Connect this wrapper to the given transport and initialize the
necessary L{OpenSSL.SSL.Connection} with a memory BIO.
"""
tlsContext = self.factory._contextFactory.getContext()
self._tlsConnection = Connection(tlsContext, None)
if self.factory._isClient:
self._tlsConnection.set_connect_state()
else:
self._tlsConnection.set_accept_state()
self._appSendBuffer = []
# Intentionally skip ProtocolWrapper.makeConnection - it might call
# wrappedProtocol.makeConnection, which we want to make conditional.
Protocol.makeConnection(self, transport)
self.factory.registerProtocol(self)
if self._connectWrapped:
# Now that the TLS layer is initialized, notify the application of
# the connection.
ProtocolWrapper.makeConnection(self, transport)
# Now that we ourselves have a transport (initialized by the
# ProtocolWrapper.makeConnection call above), kick off the TLS
# handshake.
try:
self._tlsConnection.do_handshake()
except WantReadError:
# This is the expected case - there's no data in the connection's
# input buffer yet, so it won't be able to complete the whole
# handshake now. If this is the speak-first side of the
# connection, then some bytes will be in the send buffer now; flush
# them.
self._flushSendBIO()
示例8: connectionMade
def connectionMade(self):
"""
Log the new connection and initialize the buffer list.
"""
ProtocolWrapper.connectionMade(self)
log.msg("Opening connection with %s" % self.transport.getPeer())
self._buffer = []
示例9: _parseFrames
def _parseFrames(self):
"""
Find frames in incoming data and pass them to the underlying protocol.
"""
try:
frames, rest = _parseFrames("".join(self._buffer))
except _WSException:
# Couldn't parse all the frames, something went wrong, let's bail.
log.err()
self.loseConnection()
return
self._buffer[:] = [rest]
for frame in frames:
opcode, data = frame
if opcode == _CONTROLS.NORMAL:
# Business as usual. Decode the frame, if we have a decoder.
# Pass the frame to the underlying protocol.
ProtocolWrapper.dataReceived(self, data)
elif opcode == _CONTROLS.CLOSE:
# The other side wants us to close. I wonder why?
reason, text = data
log.msg("Closing connection: %r (%d)" % (text, reason))
# Close the connection.
self.loseConnection()
return
elif opcode == _CONTROLS.PING:
# 5.5.2 PINGs must be responded to with PONGs.
# 5.5.3 PONGs must contain the data that was sent with the
# provoking PING.
self.transport.write(_makeFrame(data, _opcode=_CONTROLS.PONG))
示例10: connectionLost
def connectionLost(self, reason):
# type: (AnyStr) -> None
# reason = [Failure instance: Traceback (failure with no
# frames): <class 'twisted.internet.error.ConnectionDone'>:
# Connection was closed cleanly. ] (<type 'instance'>)
self.clear()
ProtocolWrapper.connectionLost(self, reason)
示例11: writeSequence
def writeSequence(self, seq):
if not self.tlsStarted:
ProtocolWrapper.writeSequence(self, seq)
else:
#Because of the FakeSocket, write operations are guaranteed to
#terminate immediately.
AsyncStateMachine.setWriteOp(self, "".join(seq))
示例12: makeConnection
def makeConnection(self, transport):
"""
Fire the Deferred at C{self.factory.connectionNotification} with the
real protocol.
"""
ProtocolWrapper.makeConnection(self, transport)
self.factory.connectionNotification.callback(self.wrappedProtocol)
示例13: parseFrames
def parseFrames(self):
"""
Find frames in incoming data and pass them to the underlying protocol.
"""
try:
frames, self.buf = parse_hybi07_frames(self.buf)
except WSException:
# Couldn't parse all the frames, something went wrong, let's bail.
log.err()
self.loseConnection()
return
for frame in frames:
opcode, data = frame
if opcode == NORMAL:
# Business as usual. Decode the frame, if we have a decoder.
if self.codec:
data = decoders[self.codec](data)
# Pass the frame to the underlying protocol.
ProtocolWrapper.dataReceived(self, data)
elif opcode == CLOSE:
# The other side wants us to close. I wonder why?
reason, text = data
log.msg("Closing connection: %r (%d)" % (text, reason))
# Close the connection.
self.loseConnection()
return
elif opcode == PING:
# 5.5.2 PINGs must be responded to with PONGs.
# 5.5.3 PONGs must contain the data that was sent with the
# provoking PING.
raise AssertionError("this doesn't work") # due to unknown symbol below
示例14: __init__
def __init__(self, factory, wrappedProtocol, connectedDeferred, host, port, optimistic = False):
ProtocolWrapper.__init__(self, factory, wrappedProtocol)
self._connectedDeferred = connectedDeferred
self._host = host
self._port = port
self._optimistic = optimistic
self._buf = ''
self.state = 0
示例15: writeSequence
def writeSequence(self, data):
if debug:
print 'TwistedProtocolWrapper.writeSequence'
if not self.tlsStarted:
ProtocolWrapper.writeSequence(self, ''.join(data))
return
self.write(''.join(data))