本文整理汇总了Python中twisted.internet.protocol.ClientFactory.buildProtocol方法的典型用法代码示例。如果您正苦于以下问题:Python ClientFactory.buildProtocol方法的具体用法?Python ClientFactory.buildProtocol怎么用?Python ClientFactory.buildProtocol使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.internet.protocol.ClientFactory
的用法示例。
在下文中一共展示了ClientFactory.buildProtocol方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: buildProtocol
# 需要导入模块: from twisted.internet.protocol import ClientFactory [as 别名]
# 或者: from twisted.internet.protocol.ClientFactory import buildProtocol [as 别名]
def buildProtocol(self, address):
""" Overriden method to distinguish between the two protocols. """
if address.port == 8888:
self.protocol = DirectoryProtocol
self.dirProto = ClientFactory.buildProtocol(self, address)
return self.dirProto
else:
self.protocol = PeerProtocol
return ClientFactory.buildProtocol(self, address)
示例2: buildProtocol
# 需要导入模块: from twisted.internet.protocol import ClientFactory [as 别名]
# 或者: from twisted.internet.protocol.ClientFactory import buildProtocol [as 别名]
def buildProtocol(self, addr):
log.msg("Building protocol for %r" % addr)
proto = ClientFactory.buildProtocol(self, addr)
proto.app = self.app
self.proto = proto
self.join.ready()
return proto
示例3: test_send_peh_upon_connection
# 需要导入模块: from twisted.internet.protocol import ClientFactory [as 别名]
# 或者: from twisted.internet.protocol.ClientFactory import buildProtocol [as 别名]
def test_send_peh_upon_connection(self):
'''To test client protocol we isloate it from the ClientFactory'''
with patch.object(datetime, 'datetime', Mock(wraps=datetime.datetime)) as patched:
fixed_date = datetime.datetime(2014, 1, 1, 12, 0, 0)
patched.now.return_value = fixed_date
factory = ClientFactory()
factory.comaster = self.comaster
factory.protocol = MaraClientProtocol
proto = factory.buildProtocol(('127.0.0.1', 0))
proto.construct = MaraFrame
# Disable unnesesary behaviour
def stop():
proto.stop()
reactor.stop()
proto.sendPoll = MagicMock(side_effect=stop)
transport = proto_helpers.StringTransport()
proto.makeConnection(transport)
bytes_sent_to_device = transport.value()
result = MaraFrame.parse(bytes_sent_to_device)
self.assertEqual(result.dest, 0xFF)
self.assertEqual(result.source, 2)
# We don't need to check BCC since it's already coded into MaraFrame
self.assertEqual(result.peh, fixed_date)
reactor.run()
# Shuld have stopped
self.assertEqual(self.comaster.update_peh_timestamp.call_count, 1)
self.assertEqual(self.comaster.update_peh_timestamp.call_args[0][0],
fixed_date)
示例4: test_connectTCP
# 需要导入模块: from twisted.internet.protocol import ClientFactory [as 别名]
# 或者: from twisted.internet.protocol.ClientFactory import buildProtocol [as 别名]
def test_connectTCP(self):
"""
Called on the object returned by L{loggedReactor}, C{connectTCP} calls
the wrapped reactor's C{connectTCP} method with the original factory
wrapped in a L{_TrafficLoggingFactory}.
"""
class RecordDataProtocol(Protocol):
def dataReceived(self, data):
self.data = data
proto = RecordDataProtocol()
factory = ClientFactory()
factory.protocol = lambda: proto
reactor = MemoryReactor()
logged = loggedReactor(reactor)
logged.connectTCP('192.168.1.2', 1234, factory, 21, '127.0.0.2')
[(host, port, factory, timeout, bindAddress)] = reactor.tcpClients
self.assertEqual('192.168.1.2', host)
self.assertEqual(1234, port)
self.assertIsInstance(factory, _TrafficLoggingFactory)
self.assertEqual(21, timeout)
self.assertEqual('127.0.0.2', bindAddress)
# Verify that the factory and protocol specified are really being used
protocol = factory.buildProtocol(None)
protocol.makeConnection(None)
protocol.dataReceived("foo")
self.assertEqual(proto.data, "foo")
示例5: testStor
# 需要导入模块: from twisted.internet.protocol import ClientFactory [as 别名]
# 或者: from twisted.internet.protocol.ClientFactory import buildProtocol [as 别名]
def testStor(self):
# Connect
client = ftp.FTPClient(passive=self.passive)
client.debug = 1
factory = ClientFactory()
factory.noisy = 0
factory.buildProtocol = lambda s, c=client: c
reactor.connectTCP('localhost', self.ftp_port, factory)
expectedContent = "Hello\n"*4
def gotResult(c):
c.write(expectedContent)
c.finish()
def gotErr(f):
self.errback(f)
t = client.storeFile("HelloThere")
t[0].addCallbacks(gotResult, gotErr)
t[1].addCallbacks(self.callback, self.errback)
# Wait for a result
id = reactor.callLater(5, self.errback, "timed out") # timeout so we don't freeze
while not hasattr(self, 'result') and not hasattr(self, 'error'):
reactor.iterate()
try:
id.cancel()
except ValueError: pass
error = getattr(self, 'error', None)
if error:
raise error[0], error[1], error[2]
self.assertEquals(open('HelloThere').read(), expectedContent)
示例6: testRetr
# 需要导入模块: from twisted.internet.protocol import ClientFactory [as 别名]
# 或者: from twisted.internet.protocol.ClientFactory import buildProtocol [as 别名]
def testRetr(self):
# Connect
client = ftp.FTPClient(passive=self.passive)
factory = ClientFactory()
factory.noisy = 0
factory.buildProtocol = lambda s, c=client: c
reactor.connectTCP('localhost', self.ftp_port, factory)
# download ftp_crap
proto = BufferingProtocol()
d = client.retr(os.path.basename('ftp_crap'), proto)
d.addCallbacks(self.callback, self.errback)
# Wait for a result
id = reactor.callLater(5, self.errback, "timed out") # timeout so we don't freeze
while not hasattr(self, 'result') and not hasattr(self, 'error'):
reactor.iterate()
try:
id.cancel()
except ValueError: pass
error = getattr(self, 'error', None)
if error:
raise error[0], error[1], error[2]
# Check that the file is the same as read directly off the disk
self.failUnless(type(self.result) == types.ListType,
'callback result is wrong type: ' + str(self.result))
data = proto.buf.getvalue()
self.failUnless(data == open('ftp_crap', "rb").read(),
'RETRieved file does not match original')
示例7: testShortFileListings
# 需要导入模块: from twisted.internet.protocol import ClientFactory [as 别名]
# 或者: from twisted.internet.protocol.ClientFactory import buildProtocol [as 别名]
def testShortFileListings(self):
# Connect
client = ftp.FTPClient(passive=self.passive)
factory = ClientFactory()
factory.noisy = 0
factory.buildProtocol = lambda s, c=client: c
reactor.connectTCP('localhost', self.ftp_port, factory)
# Issue the command and set the callbacks
p = BufferingProtocol()
d = client.nlst('.', p)
d.addCallbacks(self.callback, self.errback)
# Wait for the result
id = reactor.callLater(5, self.errback, "timed out") # timeout so we don't freeze
while not hasattr(self, 'result') and not hasattr(self, 'error'):
reactor.iterate()
try:
id.cancel()
except ValueError: pass
error = getattr(self, 'error', None)
if error:
raise error[0], error[1], error[2]
# Check that the listing contains this file (ftp_crap)
filenames = p.buf.getvalue().split('\r\n')
self.failUnless('ftp_crap' in filenames,
'ftp_crap not in file listing')
示例8: buildProtocol
# 需要导入模块: from twisted.internet.protocol import ClientFactory [as 别名]
# 或者: from twisted.internet.protocol.ClientFactory import buildProtocol [as 别名]
def buildProtocol(self, addr):
"""Provision protocol with the dedicated logger
"""
proto = ClientFactory.buildProtocol(self, addr)
proto.log = self.log
return proto
示例9: buildProtocol
# 需要导入模块: from twisted.internet.protocol import ClientFactory [as 别名]
# 或者: from twisted.internet.protocol.ClientFactory import buildProtocol [as 别名]
def buildProtocol(self, addr):
p = ClientFactory.buildProtocol(self, addr)
if not hasattr(self,'deferred'):
self.deferred = defer.Deferred()
self.reactor.callLater(0, self.deferred.callback, p)
del self.deferred
return p
示例10: SpdyProtocolTest
# 需要导入模块: from twisted.internet.protocol import ClientFactory [as 别名]
# 或者: from twisted.internet.protocol.ClientFactory import buildProtocol [as 别名]
class SpdyProtocolTest(TestCase):
def setUp(self):
self.factory = ClientFactory()
self.factory.protocol = spdy.SpdyProtocol
self.proto = self.factory.buildProtocol(('127.0.0.1', 0))
self.tr = proto_helpers.StringTransport()
self.proto.makeConnection(self.tr)
def tearDown(self):
return self.tr.loseConnection()
def send(self, data):
self.proto.dataReceived(data)
def testExampleFrames(self):
called = []
def request(streamId, headers):
called.append(None)
self.assertEqual(1, streamId)
self.assertEqual('GET', headers.getRawHeaders('method')[0])
self.proto.requestFactory = request
self.send(example_frames)
self.assertEqual(1, len(called))
示例11: buildProtocol
# 需要导入模块: from twisted.internet.protocol import ClientFactory [as 别名]
# 或者: from twisted.internet.protocol.ClientFactory import buildProtocol [as 别名]
def buildProtocol(self, addr):
"""
Instantiate sub protocol.
"""
p = ClientFactory.buildProtocol(self, addr)
p.initialAction = self.deferreds.pop(0)
return p
示例12: buildProtocol
# 需要导入模块: from twisted.internet.protocol import ClientFactory [as 别名]
# 或者: from twisted.internet.protocol.ClientFactory import buildProtocol [as 别名]
def buildProtocol(self, address):
logger.vdebug(
'Building protocol in StarboundClientFactory to address %s',
address
)
protocol = ClientFactory.buildProtocol(self, address)
protocol.server_protocol = self.server_protocol
return protocol
示例13: buildProtocol
# 需要导入模块: from twisted.internet.protocol import ClientFactory [as 别名]
# 或者: from twisted.internet.protocol.ClientFactory import buildProtocol [as 别名]
def buildProtocol(self, addr):
r = ClientFactory.buildProtocol(self, addr)
r.postHandshakeEndpoint = self.postHandshakeEndpoint
r.postHandshakeFactory = self.postHandshakeFactory
r.handshakeDone = self.handshakeDone
r._timestamps = self._timestamps
r._timer = self._timer
return r
示例14: makeProto
# 需要导入模块: from twisted.internet.protocol import ClientFactory [as 别名]
# 或者: from twisted.internet.protocol.ClientFactory import buildProtocol [as 别名]
def makeProto(self, *a, **kw):
protoClass = kw.pop('_protoClass', self.protocol)
fac = ClientFactory(*a, **kw)
fac.protocol = protoClass
proto = fac.buildProtocol(None)
transport = proto_helpers.StringTransport()
transport.abortConnection = lambda: None
proto.makeConnection(transport)
return fac, proto
示例15: buildProtocol
# 需要导入模块: from twisted.internet.protocol import ClientFactory [as 别名]
# 或者: from twisted.internet.protocol.ClientFactory import buildProtocol [as 别名]
def buildProtocol(self, addr):
"""Provision protocol
"""
proto = ClientFactory.buildProtocol(self, addr)
# Setup logger
proto.log = self.log
return proto