本文整理匯總了Python中twisted.internet.protocol.Factory.forProtocol方法的典型用法代碼示例。如果您正苦於以下問題:Python Factory.forProtocol方法的具體用法?Python Factory.forProtocol怎麽用?Python Factory.forProtocol使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類twisted.internet.protocol.Factory
的用法示例。
在下文中一共展示了Factory.forProtocol方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: connect
# 需要導入模塊: from twisted.internet.protocol import Factory [as 別名]
# 或者: from twisted.internet.protocol.Factory import forProtocol [as 別名]
def connect(self, reconnecting=False):
if self.connecting and not reconnecting:
return
self.connecting = True
end_point = TCP4ClientEndpoint(reactor, self.host, self.port, 10)
d = end_point.connect(Factory.forProtocol(GraphiteProtocol))
def success(connection):
self.connecting = False
log.info('Connected to {replica}', replica=self)
self.connection = connection
def failed(error):
log.error('Connect to {replica} failed: {error}', replica=self, error=error)
reactor.callLater(10, self.connect, True)
d.addCallbacks(success, failed)
示例2: test_deferBadEncodingToConnect
# 需要導入模塊: from twisted.internet.protocol import Factory [as 別名]
# 或者: from twisted.internet.protocol.Factory import forProtocol [as 別名]
def test_deferBadEncodingToConnect(self):
"""
Since any client of L{IStreamClientEndpoint} needs to handle Deferred
failures from C{connect}, L{HostnameEndpoint}'s constructor will not
raise exceptions when given bad host names, instead deferring to
returning a failing L{Deferred} from C{connect}.
"""
endpoint = endpoints.HostnameEndpoint(
deterministicResolvingReactor(MemoryReactor(), ['127.0.0.1']),
b'\xff-garbage-\xff', 80
)
deferred = endpoint.connect(Factory.forProtocol(Protocol))
err = self.failureResultOf(deferred, ValueError)
self.assertIn("\\xff-garbage-\\xff", str(err))
endpoint = endpoints.HostnameEndpoint(
deterministicResolvingReactor(MemoryReactor(), ['127.0.0.1']),
u'\u2ff0-garbage-\u2ff0', 80
)
deferred = endpoint.connect(Factory())
err = self.failureResultOf(deferred, ValueError)
self.assertIn("\\u2ff0-garbage-\\u2ff0", str(err))
示例3: test_validUNIXHeaderResolves_getPeerHost
# 需要導入模塊: from twisted.internet.protocol import Factory [as 別名]
# 或者: from twisted.internet.protocol.Factory import forProtocol [as 別名]
def test_validUNIXHeaderResolves_getPeerHost(self):
"""
Test if UNIX headers result in the correct host and peer data.
"""
factory = HAProxyWrappingFactory(Factory.forProtocol(StaticProtocol))
proto = factory.buildProtocol(
address.UNIXAddress(b'/home/test/sockets/server.sock'),
)
transport = StringTransportWithDisconnection()
proto.makeConnection(transport)
proto.dataReceived(self.UNIXHEADER)
self.assertEqual(proto.getPeer().name, b'/home/tests/mysockets/sock')
self.assertEqual(
proto.wrappedProtocol.transport.getPeer().name,
b'/home/tests/mysockets/sock',
)
self.assertEqual(proto.getHost().name, b'/home/tests/mysockets/sock')
self.assertEqual(
proto.wrappedProtocol.transport.getHost().name,
b'/home/tests/mysockets/sock',
)
示例4: connectableEndpoint
# 需要導入模塊: from twisted.internet.protocol import Factory [as 別名]
# 或者: from twisted.internet.protocol.Factory import forProtocol [as 別名]
def connectableEndpoint(debug=False):
"""
Create an endpoint that can be fired on demand.
@param debug: A flag; whether to dump output from the established
connection to stdout.
@type debug: L{bool}
@return: A client endpoint, and an object that will cause one of the
L{Deferred}s returned by that client endpoint.
@rtype: 2-L{tuple} of (L{IStreamClientEndpoint}, L{ConnectionCompleter})
"""
reactor = MemoryReactorClock()
clientEndpoint = TCP4ClientEndpoint(reactor, "0.0.0.0", 4321)
serverEndpoint = TCP4ServerEndpoint(reactor, 4321)
serverEndpoint.listen(Factory.forProtocol(Protocol))
return clientEndpoint, ConnectionCompleter(reactor)
示例5: addCluster
# 需要導入模塊: from twisted.internet.protocol import Factory [as 別名]
# 或者: from twisted.internet.protocol.Factory import forProtocol [as 別名]
def addCluster(self, protocol):
"""Add a new stub cluster using the given `protocol`.
The `protocol` should be an instance of `amp.AMP`.
:return: py:class:`twisted.test.iosim.IOPump`
"""
server_factory = Factory.forProtocol(RegionServer)
server_factory.service = self.rpc
server = server_factory.buildProtocol(addr=None)
return iosim.connect(
server,
iosim.makeFakeServer(server),
protocol,
iosim.makeFakeClient(protocol),
debug=False, # Debugging is useful, but too noisy by default.
)
示例6: setUp
# 需要導入模塊: from twisted.internet.protocol import Factory [as 別名]
# 或者: from twisted.internet.protocol.Factory import forProtocol [as 別名]
def setUp(self):
self.reactor = MemoryProcessReactor()
self.endpoint = endpoints.ProcessEndpoint(self.reactor,
b'/bin/executable')
protocol = self.successResultOf(
self.endpoint.connect(Factory.forProtocol(Protocol))
)
self.process = self.reactor.processTransport
self.endpointTransport = protocol.transport
示例7: test_invalidHeaderDisconnects
# 需要導入模塊: from twisted.internet.protocol import Factory [as 別名]
# 或者: from twisted.internet.protocol.Factory import forProtocol [as 別名]
def test_invalidHeaderDisconnects(self):
"""
Test if invalid headers result in connectionLost events.
"""
factory = HAProxyWrappingFactory(Factory.forProtocol(StaticProtocol))
proto = factory.buildProtocol(
address.IPv4Address('TCP', b'127.1.1.1', 8080),
)
transport = StringTransportWithDisconnection()
transport.protocol = proto
proto.makeConnection(transport)
proto.dataReceived(b'NOTPROXY anything can go here\r\n')
self.assertFalse(transport.connected)
示例8: test_invalidPartialHeaderDisconnects
# 需要導入模塊: from twisted.internet.protocol import Factory [as 別名]
# 或者: from twisted.internet.protocol.Factory import forProtocol [as 別名]
def test_invalidPartialHeaderDisconnects(self):
"""
Test if invalid headers result in connectionLost events.
"""
factory = HAProxyWrappingFactory(Factory.forProtocol(StaticProtocol))
proto = factory.buildProtocol(
address.IPv4Address('TCP', b'127.1.1.1', 8080),
)
transport = StringTransportWithDisconnection()
transport.protocol = proto
proto.makeConnection(transport)
proto.dataReceived(b'PROXY TCP4 1.1.1.1\r\n')
proto.dataReceived(b'2.2.2.2 8080\r\n')
self.assertFalse(transport.connected)
示例9: test_validIPv6HeaderResolves_getPeerHost
# 需要導入模塊: from twisted.internet.protocol import Factory [as 別名]
# 或者: from twisted.internet.protocol.Factory import forProtocol [as 別名]
def test_validIPv6HeaderResolves_getPeerHost(self):
"""
Test if IPv6 headers result in the correct host and peer data.
"""
factory = HAProxyWrappingFactory(Factory.forProtocol(StaticProtocol))
proto = factory.buildProtocol(
address.IPv6Address('TCP', b'::1', 8080),
)
transport = StringTransportWithDisconnection()
proto.makeConnection(transport)
proto.dataReceived(b'PROXY TCP6 ::1 ::2 8080 8888\r\n')
self.assertEqual(proto.getPeer().host, b'::1')
self.assertEqual(proto.getPeer().port, 8080)
self.assertEqual(
proto.wrappedProtocol.transport.getPeer().host,
b'::1',
)
self.assertEqual(
proto.wrappedProtocol.transport.getPeer().port,
8080,
)
self.assertEqual(proto.getHost().host, b'::2')
self.assertEqual(proto.getHost().port, 8888)
self.assertEqual(
proto.wrappedProtocol.transport.getHost().host,
b'::2',
)
self.assertEqual(
proto.wrappedProtocol.transport.getHost().port,
8888,
)
示例10: test_overflowBytesSentToWrappedProtocol
# 需要導入模塊: from twisted.internet.protocol import Factory [as 別名]
# 或者: from twisted.internet.protocol.Factory import forProtocol [as 別名]
def test_overflowBytesSentToWrappedProtocol(self):
"""
Test if non-header bytes are passed to the wrapped protocol.
"""
factory = HAProxyWrappingFactory(Factory.forProtocol(StaticProtocol))
proto = factory.buildProtocol(
address.IPv6Address('TCP', b'::1', 8080),
)
transport = StringTransportWithDisconnection()
proto.makeConnection(transport)
proto.dataReceived(b'PROXY TCP6 ::1 ::2 8080 8888\r\nHTTP/1.1 / GET')
self.assertEqual(proto.wrappedProtocol.data, b'HTTP/1.1 / GET')
示例11: test_overflowBytesSentToWrappedProtocolChunks
# 需要導入模塊: from twisted.internet.protocol import Factory [as 別名]
# 或者: from twisted.internet.protocol.Factory import forProtocol [as 別名]
def test_overflowBytesSentToWrappedProtocolChunks(self):
"""
Test if header streaming passes extra data appropriately.
"""
factory = HAProxyWrappingFactory(Factory.forProtocol(StaticProtocol))
proto = factory.buildProtocol(
address.IPv6Address('TCP', b'::1', 8080),
)
transport = StringTransportWithDisconnection()
proto.makeConnection(transport)
proto.dataReceived(b'PROXY TCP6 ::1 ::2 ')
proto.dataReceived(b'8080 8888\r\nHTTP/1.1 / GET')
self.assertEqual(proto.wrappedProtocol.data, b'HTTP/1.1 / GET')
示例12: test_overflowBytesSentToWrappedProtocolAfter
# 需要導入模塊: from twisted.internet.protocol import Factory [as 別名]
# 或者: from twisted.internet.protocol.Factory import forProtocol [as 別名]
def test_overflowBytesSentToWrappedProtocolAfter(self):
"""
Test if wrapper writes all data to wrapped protocol after parsing.
"""
factory = HAProxyWrappingFactory(Factory.forProtocol(StaticProtocol))
proto = factory.buildProtocol(
address.IPv6Address('TCP', b'::1', 8080),
)
transport = StringTransportWithDisconnection()
proto.makeConnection(transport)
proto.dataReceived(b'PROXY TCP6 ::1 ::2 ')
proto.dataReceived(b'8080 8888\r\nHTTP/1.1 / GET')
proto.dataReceived(b'\r\n\r\n')
self.assertEqual(proto.wrappedProtocol.data, b'HTTP/1.1 / GET\r\n\r\n')