本文整理汇总了Python中twisted.protocols.loopback.loopbackAsync函数的典型用法代码示例。如果您正苦于以下问题:Python loopbackAsync函数的具体用法?Python loopbackAsync怎么用?Python loopbackAsync使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了loopbackAsync函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _greetingtest
def _greetingtest(self, write, testServer):
"""
Test one of the permutations of write/writeSequence client/server.
"""
class GreeteeProtocol(Protocol):
bytes = ""
def dataReceived(self, bytes):
self.bytes += bytes
if self.bytes == "bytes":
self.received.callback(None)
class GreeterProtocol(Protocol):
def connectionMade(self):
getattr(self.transport, write)("bytes")
if testServer:
server = GreeterProtocol()
client = GreeteeProtocol()
d = client.received = Deferred()
else:
server = GreeteeProtocol()
d = server.received = Deferred()
client = GreeterProtocol()
loopback.loopbackAsync(server, client)
return d
示例2: _greetingtest
def _greetingtest(self, write, testServer):
"""
Test one of the permutations of write/writeSequence client/server.
@param write: The name of the method to test, C{"write"} or
C{"writeSequence"}.
"""
class GreeteeProtocol(Protocol):
bytes = b""
def dataReceived(self, bytes):
self.bytes += bytes
if self.bytes == b"bytes":
self.received.callback(None)
class GreeterProtocol(Protocol):
def connectionMade(self):
if write == "write":
self.transport.write(b"bytes")
else:
self.transport.writeSequence([b"byt", b"es"])
if testServer:
server = GreeterProtocol()
client = GreeteeProtocol()
d = client.received = Deferred()
else:
server = GreeteeProtocol()
d = server.received = Deferred()
client = GreeterProtocol()
loopback.loopbackAsync(server, client)
return d
示例3: _hostpeertest
def _hostpeertest(self, get, testServer):
"""
Test one of the permutations of client/server host/peer.
"""
class TestProtocol(Protocol):
def makeConnection(self, transport):
Protocol.makeConnection(self, transport)
self.onConnection.callback(transport)
if testServer:
server = TestProtocol()
d = server.onConnection = Deferred()
client = Protocol()
else:
server = Protocol()
client = TestProtocol()
d = client.onConnection = Deferred()
loopback.loopbackAsync(server, client)
def connected(transport):
host = getattr(transport, get)()
self.failUnless(IAddress.providedBy(host))
return d.addCallback(connected)
示例4: setUp
def setUp(self):
MorbidTestCase.setUp(self)
self.serverProtocol2 = self.factory.buildProtocol(None)
self.serverProtocol2.log = self.log
self.client2 = StompClientProtocol('user2','user2')
self.client2.log = self.log
self.loopbackBody = loopback.loopbackAsync(self.serverProtocol2,
self.client2)
self.serverProtocol3 = self.factory.buildProtocol(None)
self.serverProtocol3.log = self.log
self.client3 = StompClientProtocol('user3','user3')
self.client3.log = self.log
self.loopbackBody = loopback.loopbackAsync(self.serverProtocol3,
self.client3)
示例5: test_makeConnection
def test_makeConnection(self):
"""
Test that the client and server protocol both have makeConnection
invoked on them by loopbackAsync.
"""
class TestProtocol(Protocol):
transport = None
def makeConnection(self, transport):
self.transport = transport
server = TestProtocol()
client = TestProtocol()
loopback.loopbackAsync(server, client)
self.failIfEqual(client.transport, None)
self.failIfEqual(server.transport, None)
示例6: test_List
def test_List(self):
"""
You can get a listing
"""
server = Server(FakePlumber({
'ls': [
('foo', 'bar', 12, True),
]
}))
client = SingleCommandClient(List)
from twisted.protocols.loopback import loopbackAsync
def check(response):
self.assertEqual(server.plumber.called, ['ls'])
self.assertEqual(client.response, {
'pipes': [
{
'src': 'foo',
'dst': 'bar',
'conns': 12,
'active': True,
},
]
})
r = loopbackAsync(server, client)
return r.addCallback(check)
示例7: test_disorderlyShutdown
def test_disorderlyShutdown(self):
"""
If a L{TLSMemoryBIOProtocol} loses its connection unexpectedly, this is
reported to the application.
"""
clientConnectionLost = Deferred()
clientFactory = ClientFactory()
clientFactory.protocol = (
lambda: ConnectionLostNotifyingProtocol(
clientConnectionLost))
clientContextFactory = HandshakeCallbackContextFactory()
wrapperFactory = TLSMemoryBIOFactory(
clientContextFactory, True, clientFactory)
sslClientProtocol = wrapperFactory.buildProtocol(None)
# Client speaks first, so the server can be dumb.
serverProtocol = Protocol()
connectionDeferred = loopbackAsync(serverProtocol, sslClientProtocol)
# Now destroy the connection.
serverProtocol.transport.loseConnection()
# And when the connection completely dies, check the reason.
def cbDisconnected(clientProtocol):
clientProtocol.lostConnectionReason.trap(Error)
clientConnectionLost.addCallback(cbDisconnected)
return clientConnectionLost
示例8: test_pumpPolicy
def test_pumpPolicy(self):
"""
The callable passed as the value for the C{pumpPolicy} parameter to
L{loopbackAsync} is called with a L{_LoopbackQueue} of pending bytes
and a protocol to which they should be delivered.
"""
pumpCalls = []
def dummyPolicy(queue, target):
bytes = []
while queue:
bytes.append(queue.get())
pumpCalls.append((target, bytes))
client = Protocol()
server = Protocol()
finished = loopback.loopbackAsync(server, client, dummyPolicy)
self.assertEqual(pumpCalls, [])
client.transport.write(b"foo")
client.transport.write(b"bar")
server.transport.write(b"baz")
server.transport.write(b"quux")
server.transport.loseConnection()
def cbComplete(ignored):
self.assertEqual(
pumpCalls,
# The order here is somewhat arbitrary. The implementation
# happens to always deliver data to the client first.
[(client, [b"baz", b"quux", None]),
(server, [b"foo", b"bar"])])
finished.addCallback(cbComplete)
return finished
示例9: test_writeSequence
def test_writeSequence(self):
"""
Bytes written to L{TLSMemoryBIOProtocol} with C{writeSequence} are
received by the protocol on the other side of the connection.
"""
bytes = "some bytes"
class SimpleSendingProtocol(Protocol):
def connectionMade(self):
self.transport.writeSequence(list(bytes))
clientFactory = ClientFactory()
clientFactory.protocol = SimpleSendingProtocol
clientContextFactory = HandshakeCallbackContextFactory()
wrapperFactory = TLSMemoryBIOFactory(
clientContextFactory, True, clientFactory)
sslClientProtocol = wrapperFactory.buildProtocol(None)
serverProtocol = AccumulatingProtocol(len(bytes))
serverFactory = ServerFactory()
serverFactory.protocol = lambda: serverProtocol
serverContextFactory = DefaultOpenSSLContextFactory(certPath, certPath)
wrapperFactory = TLSMemoryBIOFactory(
serverContextFactory, False, serverFactory)
sslServerProtocol = wrapperFactory.buildProtocol(None)
connectionDeferred = loopbackAsync(sslServerProtocol, sslClientProtocol)
# Wait for the connection to end, then make sure the server received
# the bytes sent by the client.
def cbConnectionDone(ignored):
self.assertEquals("".join(serverProtocol.received), bytes)
connectionDeferred.addCallback(cbConnectionDone)
return connectionDeferred
示例10: writeBeforeHandshakeTest
def writeBeforeHandshakeTest(self, sendingProtocol, bytes):
"""
Run test where client sends data before handshake, given the sending
protocol and expected bytes.
"""
clientFactory = ClientFactory()
clientFactory.protocol = sendingProtocol
clientContextFactory, handshakeDeferred = (
HandshakeCallbackContextFactory.factoryAndDeferred())
wrapperFactory = TLSMemoryBIOFactory(
clientContextFactory, True, clientFactory)
sslClientProtocol = wrapperFactory.buildProtocol(None)
serverProtocol = AccumulatingProtocol(len(bytes))
serverFactory = ServerFactory()
serverFactory.protocol = lambda: serverProtocol
serverContextFactory = DefaultOpenSSLContextFactory(certPath, certPath)
wrapperFactory = TLSMemoryBIOFactory(
serverContextFactory, False, serverFactory)
sslServerProtocol = wrapperFactory.buildProtocol(None)
connectionDeferred = loopbackAsync(sslServerProtocol, sslClientProtocol)
# Wait for the connection to end, then make sure the server received
# the bytes sent by the client.
def cbConnectionDone(ignored):
self.assertEqual("".join(serverProtocol.received), bytes)
connectionDeferred.addCallback(cbConnectionDone)
return connectionDeferred
示例11: test_handshake
def test_handshake(self):
"""
The TLS handshake is performed when L{TLSMemoryBIOProtocol} is
connected to a transport.
"""
clientFactory = ClientFactory()
clientFactory.protocol = Protocol
clientContextFactory, handshakeDeferred = (
HandshakeCallbackContextFactory.factoryAndDeferred())
wrapperFactory = TLSMemoryBIOFactory(
clientContextFactory, True, clientFactory)
sslClientProtocol = wrapperFactory.buildProtocol(None)
serverFactory = ServerFactory()
serverFactory.protocol = Protocol
serverContextFactory = DefaultOpenSSLContextFactory(certPath, certPath)
wrapperFactory = TLSMemoryBIOFactory(
serverContextFactory, False, serverFactory)
sslServerProtocol = wrapperFactory.buildProtocol(None)
connectionDeferred = loopbackAsync(sslServerProtocol, sslClientProtocol)
# Only wait for the handshake to complete. Anything after that isn't
# important here.
return handshakeDeferred
示例12: test_writeNotReentrant
def test_writeNotReentrant(self):
"""
L{loopback.loopbackAsync} does not call a protocol's C{dataReceived}
method while that protocol's transport's C{write} method is higher up
on the stack.
"""
class Server(Protocol):
def dataReceived(self, bytes):
self.transport.write(b"bytes")
class Client(Protocol):
ready = False
def connectionMade(self):
reactor.callLater(0, self.go)
def go(self):
self.transport.write(b"foo")
self.ready = True
def dataReceived(self, bytes):
self.wasReady = self.ready
self.transport.loseConnection()
server = Server()
client = Client()
d = loopback.loopbackAsync(client, server)
def cbFinished(ignored):
self.assertTrue(client.wasReady)
d.addCallback(cbFinished)
return d
示例13: testEmptyPASS
def testEmptyPASS(self):
dummy = DummyPOP3()
client = LineSendingProtocol([
"PASS ",
"QUIT"
])
d = loopback.loopbackAsync(dummy, client)
return d.addCallback(self._cbTestEmptyPASS, client, dummy)
示例14: testAuthListing
def testAuthListing(self):
p = DummyPOP3()
p.factory = internet.protocol.Factory()
p.factory.challengers = {"Auth1": None, "secondAuth": None, "authLast": None}
client = LineSendingProtocol(["AUTH", "QUIT"])
d = loopback.loopbackAsync(p, client)
return d.addCallback(self._cbTestAuthListing, client)
示例15: testProtocol
def testProtocol(self):
from twisted.protocols import loopback
server = flow.Protocol()
server.controller = echoServer
client = flow.makeProtocol(echoClient)()
client.factory = protocol.ClientFactory()
client.factory.d = defer.Deferred()
d2 = loopback.loopbackAsync(server, client)
client.factory.d.addCallback(self.assertEquals, 'testing')
return defer.gatherResults([client.factory.d, d2])