Here are the examples of the python api twisted.internet.protocol.ClientCreator taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
38 Examples
3
Example 1
@inlineCallbacks
def start_client(self, port):
self.client_disconnected = Deferred()
self.client_creator = ClientCreator(reactor, self.client_protocol)
self.client = yield self.client_creator.connectTCP('127.0.0.1', port)
conn_lost = self.client.connectionLost
def connectionLost_wrapper(reason):
d = maybeDeferred(conn_lost, reason)
d.chainDeferred(self.client_disconnected)
return d
self.client.connectionLost = connectionLost_wrapper
3
Example 2
@inlineCallbacks
def make_client(self):
addr = self.worker.telnet_server.getHost()
cc = protocol.ClientCreator(reactor, ClientProtocol)
client = yield cc.connectTCP("127.0.0.1", addr.port)
returnValue(client)
3
Example 3
Project: scrapy Source File: ftp.py
def download_request(self, request, spider):
parsed_url = urlparse(request.url)
creator = ClientCreator(reactor, FTPClient, request.meta["ftp_user"],
request.meta["ftp_password"],
passive=request.meta.get("ftp_passive", 1))
return creator.connectTCP(parsed_url.hostname, parsed_url.port or 21).addCallback(self.gotClient,
request, unquote(parsed_url.path))
3
Example 4
def client():
stats = Stats()
c = protocol.ClientCreator(reactor, DumbProto)
c.connectTCP("localhost", 5333).addCallback(gotProtocol, stats, 3000)
reactor.run()
3
Example 5
def download_request(self, request, spider):
parsed_url = urlparse(request.url)
creator = ClientCreator(reactor, FTPClient, request.meta["ftp_user"],
request.meta["ftp_password"],
passive=request.meta.get("ftp_passive", 1))
return creator.connectTCP(parsed_url.hostname, parsed_url.port or 21).addCallback(self.gotClient,
request, parsed_url.path)
3
Example 6
def getTimeOffset():
client = ClientCreator(reactor, SimpleTelnet)
server = timeservers.pop(0)
logger.debug( "Requesting time from %s." % server )
d = client.connectTCP(server, 13, timeout=5)
d.addCallback( _getTimeOffsetCallback, server )
d.addErrback( _getTimeOffsetErrback, 0 )
return d
3
Example 7
Project: awspider Source File: timeoffset.py
def _getTimeOffsetErrback( error, count ):
if count < 5:
client = ClientCreator(reactor, SimpleTelnet)
server = timeservers.pop()
logger.debug( "Attempt %s failed, requesting time from %s." % (count + 1, server) )
d = client.connectTCP(server, 13, timeout=5)
d.addCallback( _getTimeOffsetCallback, server )
d.addErrback( _getTimeOffsetErrback, count + 1 )
return d
else:
logger.debug( "Could not fetch time after %s attempts." % count )
return error
3
Example 8
Project: canvas Source File: channels.py
@inlineCallbacks
def connect(self):
cc = lambda *args: protocol.ClientCreator(reactor, *args)
self.redis_sub = RedisDispatch(settings.REDIS_HOST, settings.REDIS_PORT)
redis_factory = RedisServiceRegisteringFactory(self)
reactor.connectTCP(settings.REDIS_HOST, settings.REDIS_PORT, redis_factory)
yield redis_factory.deferred
3
Example 9
Project: gemuo Source File: client.py
def connect(host, port, *args, **keywords):
d = defer.Deferred()
c = ClientCreator(reactor, UOProtocol, *args, **keywords)
e = c.connectTCP(host, port)
e.addCallback(lambda client: d.callback(Client(client)))
e.addErrback(lambda f: d.errback(f))
return d
3
Example 10
@inlineCallbacks
def login(self, user, password):
"""Login message bus
"""
self.logger.debug('Logging in as %s ...', user)
host = self.hosts[0]
delegate = TwistedDelegate()
creactor = ClientCreator(reactor, AMQClient, delegate=delegate,
vhost=self.vhost, spec=self.spec)
self.conn = yield creactor.connectTCP(host[0], host[1])
yield self.conn.authenticate(user, password)
self.channel = yield self.conn.channel(1)
yield self.channel.channel_open()
yield self.channel.exchange_declare(exchange=self.exchange_name,
type='topic')
self.logger.info('Login as %s', user)
3
Example 11
def channelOpen(self, specificData):
cc = protocol.ClientCreator(reactor, SSHAgentForwardingLocal)
d = cc.connectUNIX(os.environ['SSH_AUTH_SOCK'])
d.addCallback(self._cbGotLocal)
d.addErrback(lambda x:self.loseConnection())
self.buf = ''
3
Example 12
Project: mythbox Source File: default.py
def serviceStarted(self):
if 'SSH_AUTH_SOCK' in os.environ and not self.options['noagent']:
log.msg('using agent')
cc = protocol.ClientCreator(reactor, agent.SSHAgentClient)
d = cc.connectUNIX(os.environ['SSH_AUTH_SOCK'])
d.addCallback(self._setAgent)
d.addErrback(self._ebSetAgent)
else:
userauth.SSHUserAuthClient.serviceStarted(self)
3
Example 13
def _connect(self):
"""
Connect to the server, which is often a third-party process.
Tries to reconnect if it fails because we have no way of determining
exactly when the port becomes available for listening -- we can only
know when the process starts.
"""
cc = protocol.ClientCreator(reactor, ConchTestForwardingPort, self,
self.data)
d = cc.connectTCP('127.0.0.1', self.port)
d.addErrback(self._ebConnect)
return d
3
Example 14
Project: protobuf-rpc Source File: test_service.py
def testTcpRpc( self ):
def connected( protocol ):
self.tcp_proxy_proto = protocol
text = "TCP Test"
request = EchoRequest()
request.text = text
proxy = tx.Proxy( Test_Stub( protocol ) )
echoed = proxy.Test.Echo( request )
echoed.addCallback( lambda r: self.assertEquals( r.text, text ) )
return echoed
client = ClientCreator( reactor, tx.TcpChannel )
d = client.connectTCP( self.tcp_listener.getHost().host,
self.tcp_listener.getHost().port )
d.addCallback( connected )
return d
3
Example 15
def _startLogOn(self, chatui):
logonDeferred = defer.Deferred()
cc = protocol.ClientCreator(reactor, IRCProto, self, chatui,
logonDeferred)
d = cc.connectTCP(self.host, self.port)
d.addErrback(logonDeferred.errback)
return logonDeferred
3
Example 16
Project: SubliminalCollaborator Source File: oscar.py
def oscar_01_05(self, snac, d = None):
"""
data for a new service connection
d might be a deferred to be called back when the service is ready
"""
tlvs = readTLVs(snac[3][2:])
service = struct.unpack('!H',tlvs[0x0d])[0]
ip = tlvs[5]
cookie = tlvs[6]
#c = serviceClasses[service](self, cookie, d)
c = protocol.ClientCreator(reactor, serviceClasses[service], self, cookie, d)
def addService(x):
self.services[service] = x
c.connectTCP(ip, 5190).addCallback(addService)
0
Example 17
Project: graphite Source File: amqp_publisher.py
@inlineCallbacks
def writeMetric(metric_path, value, timestamp, host, port, username, password,
vhost, exchange, spec=None, channel_number=1, ssl=False):
if not spec:
spec = txamqp.spec.load(os.path.normpath(
os.path.join(os.path.dirname(__file__), 'amqp0-8.xml')))
delegate = TwistedDelegate()
connector = ClientCreator(reactor, AMQClient, delegate=delegate,
vhost=vhost, spec=spec)
if ssl:
from twisted.internet.ssl import ClientContextFactory
conn = yield connector.connectSSL(host, port, ClientContextFactory())
else:
conn = yield connector.connectTCP(host, port)
yield conn.authenticate(username, password)
channel = yield conn.channel(channel_number)
yield channel.channel_open()
yield channel.exchange_declare(exchange=exchange, type="topic",
durable=True, auto_delete=False)
message = Content( "%f %d" % (value, timestamp) )
message["delivery mode"] = 2
channel.basic_publish(exchange=exchange, content=message, routing_key=metric_path)
yield channel.channel_close()
0
Example 18
Project: awspider Source File: worker.py
def __init__(self,
aws_access_key_id,
aws_secret_access_key,
aws_s3_http_cache_bucket=None,
aws_s3_storage_bucket=None,
mysql_username=None,
mysql_password=None,
mysql_host=None,
mysql_database=None,
amqp_host=None,
amqp_username=None,
amqp_password=None,
amqp_vhost=None,
amqp_queue=None,
amqp_exchange=None,
memcached_host=None,
scheduler_server_group='flavors_spider_production',
schedulerserver_port=5004,
service_mapping=None,
service_args_mapping=None,
amqp_port=5672,
amqp_prefetch_count=200,
mysql_port=3306,
memcached_port=11211,
max_simultaneous_requests=100,
max_requests_per_host_per_second=0,
max_simultaneous_requests_per_host=0,
port=5005,
log_file='workerserver.log',
log_directory=None,
log_level="debug"):
self.network_information["port"] = port
# Create MySQL connection.
self.mysql = adbapi.ConnectionPool(
"MySQLdb",
db=mysql_database,
port=mysql_port,
user=mysql_username,
passwd=mysql_password,
host=mysql_host,
cp_reconnect=True,
cursorclass=DictCursor)
# Create Memcached client
self.memcached_host = memcached_host
self.memcached_port = memcached_port
self.memc_ClientCreator = protocol.ClientCreator(
reactor, MemCacheProtocol)
#Schedule Server
self.scheduler_server_group=scheduler_server_group
self.schedulerserver_port=schedulerserver_port
# Resource Mappings
self.service_mapping = service_mapping
self.service_args_mapping = service_args_mapping
# HTTP interface
resource = WorkerResource(self)
self.site_port = reactor.listenTCP(port, server.Site(resource))
# Create AMQP Connection
# AMQP connection parameters
self.amqp_host = amqp_host
self.amqp_vhost = amqp_vhost
self.amqp_port = amqp_port
self.amqp_username = amqp_username
self.amqp_password = amqp_password
self.amqp_queue = amqp_queue
self.amqp_exchange = amqp_exchange
self.amqp_prefetch_count = amqp_prefetch_count
BaseServer.__init__(
self,
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
aws_s3_http_cache_bucket=aws_s3_http_cache_bucket,
aws_s3_storage_bucket=aws_s3_storage_bucket,
scheduler_server_group=scheduler_server_group,
max_simultaneous_requests=max_simultaneous_requests,
max_requests_per_host_per_second=max_requests_per_host_per_second,
max_simultaneous_requests_per_host=max_simultaneous_requests_per_host,
log_file=log_file,
log_directory=log_directory,
log_level=log_level)
0
Example 19
Project: tensor Source File: munin.py
@defer.inlineCallbacks
def get(self):
host = self.config.get('host', 'localhost')
port = int(self.config.get('port', 4949))
creator = ClientCreator(reactor, MuninProtocol)
proto = yield creator.connectTCP(host, port)
# Announce our capabilities
yield proto.sendCommand('cap multigraph')
listout = yield proto.sendCommand('list')
plug_list = listout.split()
events = []
for plug in plug_list:
# Retrive the configuration for this plugin
config = yield proto.sendCommand('config %s' % plug, True)
plugin_config = {}
for r in config:
name, val = r.split(' ', 1)
if '.' in name:
metric, key = name.split('.')
if key in ['type', 'label', 'min', 'info']:
plugin_config['%s.%s.%s' % (plug, metric, key)] = val
else:
if name == 'graph_category':
plugin_config['%s.category' % plug] = val
category = plugin_config.get('%s.category' % plug, 'system')
# Retrieve the metrics
metrics = yield proto.sendCommand('fetch %s' % plug, True)
for m in metrics:
name, val = m.split(' ', 1)
if name != 'multigraph':
metric, key = name.split('.')
base = '%s.%s' % (plug, metric)
m_type = plugin_config.get('%s.type' % base, 'GAUGE')
try:
val = float(val)
except:
continue
base = '%s.%s' % (plug, metric)
info = plugin_config.get('%s.info' % base, base)
prefix = '%s.%s' % (category, base)
if m_type == 'COUNTER':
events.append(self.createEvent('ok', info, val,
prefix=prefix, aggregation=Counter32))
elif m_type == 'DERIVE':
events.append(self.createEvent('ok', info, val,
prefix=prefix, aggregation=Counter))
else:
events.append(self.createEvent('ok', info, val,
prefix=prefix))
yield proto.disconnect()
defer.returnValue(events)
0
Example 20
Project: conn-check Source File: checks.py
@inlineCallbacks
def do_tcp_check(host, port, tls=False, tls_verify=True,
timeout=None):
"""Generic connection check function."""
if not isIPAddress(host):
try:
ip = yield reactor.resolve(host, timeout=(1, timeout))
except DNSLookupError:
raise ValueError("dns resolution failed")
else:
ip = host
creator = ClientCreator(reactor, TCPCheckProtocol)
try:
if tls:
context = VerifyingContextFactory(tls_verify, CA_CERTS)
yield creator.connectSSL(ip, port, context,
timeout=timeout)
else:
yield creator.connectTCP(ip, port, timeout=timeout)
except TimeoutError:
if ip == host:
raise ValueError("timed out")
else:
raise ValueError("timed out connecting to {}".format(ip))
0
Example 21
Project: conn-check Source File: checks.py
def make_amqp_check(host, port, username, password, use_tls=True, vhost="/",
timeout=None, **kwargs):
"""Return a check for AMQP connectivity."""
from txamqp.protocol import AMQClient
from txamqp.client import TwistedDelegate
from txamqp.spec import load as load_spec
subchecks = []
subchecks.append(make_tcp_check(host, port, timeout=timeout))
if use_tls:
subchecks.append(make_tls_check(host, port, verify=False,
timeout=timeout))
@inlineCallbacks
def do_auth():
"""Connect and authenticate."""
delegate = TwistedDelegate()
spec = load_spec(resource_stream('conn_check', 'amqp0-8.xml'))
creator = ClientCreator(reactor, AMQClient,
delegate, vhost, spec)
client = yield creator.connectTCP(host, port, timeout=timeout)
yield client.authenticate(username, password)
subchecks.append(make_check("amqp:{}:{}".format(host, port),
do_auth, info="user {}".format(username),))
return sequential_check(subchecks)
0
Example 22
Project: conn-check Source File: checks.py
def make_redis_check(host, port, password=None, timeout=None,
**kwargs):
"""Make a check for the configured redis server."""
import txredis
subchecks = []
subchecks.append(make_tcp_check(host, port, timeout=timeout))
@inlineCallbacks
def do_connect():
"""Connect and authenticate.
"""
client_creator = ClientCreator(reactor, txredis.client.RedisClient)
client = yield client_creator.connectTCP(host=host, port=port,
timeout=timeout)
if password is None:
ping = yield client.ping()
if not ping:
raise RuntimeError("failed to ping redis")
else:
resp = yield client.auth(password)
if resp != 'OK':
raise RuntimeError("failed to auth to redis")
if password is not None:
connect_info = "connect with auth"
else:
connect_info = "connect"
subchecks.append(make_check(connect_info, do_connect))
return add_check_prefix('redis:{}:{}'.format(host, port),
sequential_check(subchecks))
0
Example 23
Project: conn-check Source File: checks.py
def make_memcache_check(host, port, password=None, timeout=None,
**kwargs):
"""Make a check for the configured redis server."""
subchecks = []
subchecks.append(make_tcp_check(host, port, timeout=timeout))
@inlineCallbacks
def do_connect():
"""Connect and authenticate."""
client_creator = ClientCreator(reactor, MemCacheProtocol)
client = yield client_creator.connectTCP(host=host, port=port,
timeout=timeout)
version = yield client.version()
if version is None:
raise RuntimeError('Failed retrieve memcached server version')
subchecks.append(make_check('connect', do_connect))
return add_check_prefix('memcache:{}:{}'.format(host, port),
sequential_check(subchecks))
0
Example 24
Project: Coherence Source File: event.py
def subscribe(service, action='subscribe'):
"""
send a subscribe/renewal/unsubscribe request to a service
return the device response
"""
logger = log.getLogger("event_protocol")
logger.info("event.subscribe, action: %r", action)
_, host_port, path, _, _ = urlsplit(service.get_base_url())
if host_port.find(':') != -1:
host, port = tuple(host_port.split(':'))
port = int(port)
else:
host = host_port
port = 80
def send_request(p, action):
logger.info("event.subscribe.send_request %r, action: %r %r",
p, action, service.get_event_sub_url())
_, _, event_path, _, _ = urlsplit(service.get_event_sub_url())
if action == 'subscribe':
timeout = service.timeout
if timeout == 0:
timeout = 1800
request = ["SUBSCRIBE %s HTTP/1.1" % event_path,
"HOST: %s:%d" % (host, port),
"TIMEOUT: Second-%d" % timeout,
]
service.event_connection = p
else:
request = ["UNSUBSCRIBE %s HTTP/1.1" % event_path,
"HOST: %s:%d" % (host, port),
]
if service.get_sid():
request.append("SID: %s" % service.get_sid())
else:
# XXX use address and port set in the coherence instance
#ip_address = p.transport.getHost().host
global hostname, web_server_port
#print hostname, web_server_port
url = 'http://%s:%d/events' % (hostname, web_server_port)
request.append("CALLBACK: <%s>" % url)
request.append("NT: upnp:event")
request.append('Date: %s' % datetimeToString())
request.append("Content-Length: 0")
request.append("")
request.append("")
request = '\r\n'.join(request)
logger.debug("event.subscribe.send_request %r %r", request, p)
try:
p.transport.writeSomeData(request)
except AttributeError:
logger.info("transport for event %r already gone", action)
#print "event.subscribe.send_request", d
#return d
def got_error(failure, action):
logger.info("error on %s request with %s", action, service.get_base_url())
logger.debug(failure)
def teardown_connection(c, d):
logger.info("event.subscribe.teardown_connection")
del d
del c
def prepare_connection(service, action):
logger.info("event.subscribe.prepare_connection action: %r %r",
action, service.event_connection)
if service.event_connection == None:
c = ClientCreator(reactor, EventProtocol, service=service, action=action)
logger.info("event.subscribe.prepare_connection: %r %r",
host, port)
d = c.connectTCP(host, port)
d.addCallback(send_request, action=action)
d.addErrback(got_error, action)
#reactor.callLater(3, teardown_connection, c, d)
else:
d = defer.Deferred()
d.addCallback(send_request, action=action)
d.callback(service.event_connection)
#send_request(service.event_connection, action)
return d
""" FIXME:
we need to find a way to be sure that our unsubscribe calls get through
on shutdown
reactor.addSystemEventTrigger( 'before', 'shutdown', prepare_connection, service, action)
"""
return prepare_connection(service, action)
0
Example 25
Project: nowin_core Source File: stomp.py
@inlineCallbacks
def connect(self):
"""Connect to peer
"""
from twisted.internet.protocol import ClientCreator
self.closed = False
if self.client is not None:
self.logger.warn('Already connected')
returnValue(None)
self.logger.debug('Logging in as %s ...', self.user)
creator = ClientCreator(reactor, async_client.STOMPClient)
try:
self.logger.info('Connecting to %s', self.host)
self.client = yield creator.connectTCP(*self.host)
except Exception, e:
self.logger.info('Failed to connect to %s', self.host)
self.client = None
self.conn_failed_event()
returnValue(e)
# already closed
if self.closed:
self.logger.warn('Abort connection')
self.client.close()
self.client = None
return
self._sub_ids.append(
self.client.conn_lost_event.subscribe(self.handleConnLost))
self._sub_ids.append(
self.client.auth_event.subscribe(self.handleAuth))
try:
yield self.client.login(self.user, self.password)
except Exception, e:
self.logger.info('Failed to login as %s', self.user)
self.logger.exception(e)
self.conn_failed_event()
returnValue(e)
self.logger.info('Login as %s', self.user)
0
Example 26
def channelOpen(self, specificData):
cc = protocol.ClientCreator(reactor, SSHForwardingClient, self)
log.msg("connecting to %s:%i" % self.hostport)
cc.connectTCP(*self.hostport).addCallbacks(self._setClient, self._close)
0
Example 27
Project: mythbox Source File: test_iocp.py
def test_stopStartReading(self):
"""
This test checks transport read state! There are three bits
of it:
1) The transport producer is paused -- transport.reading
is False)
2) The transport is about to schedule an OS read, on the next
reactor iteration -- transport._readScheduled
3) The OS has a pending asynchronous read on our behalf --
transport._readScheduledInOS
if 3) is not implemented, it is possible to trick IOCPReactor into
scheduling an OS read before the previous one finishes
"""
sf = ServerFactory()
sf.protocol = StopStartReadingProtocol
sf.ready_d = Deferred()
sf.stop_d = Deferred()
p = reactor.listenTCP(0, sf)
port = p.getHost().port
cc = ClientCreator(reactor, Protocol)
def proceed(protos, port):
log.msg('PROCEEDING WITH THE TESTATHRON')
self.assert_(protos[0])
self.assert_(protos[1])
protos = protos[0][1], protos[1][1]
protos[0].transport.write(
'x' * (2 * protos[0].transport.readBufferSize) +
'y' * (2 * protos[0].transport.readBufferSize))
return sf.stop_d.addCallback(cleanup, protos, port)
def cleanup(data, protos, port):
self.assert_(data == 'x'*(2*protos[0].transport.readBufferSize)+
'y'*(2*protos[0].transport.readBufferSize),
'did not get the right data')
return DeferredList([
maybeDeferred(protos[0].transport.loseConnection),
maybeDeferred(protos[1].transport.loseConnection),
maybeDeferred(port.stopListening)])
return (DeferredList([cc.connectTCP('127.0.0.1', port), sf.ready_d])
.addCallback(proceed, p))
0
Example 28
Project: mythbox Source File: test_protocol.py
def _basicConnectTest(self, check):
"""
Helper for implementing a test to verify that one of the I{connect}
methods of L{ClientCreator} passes the right arguments to the right
reactor method.
@param check: A function which will be invoked with a reactor and a
L{ClientCreator} instance and which should call one of the
L{ClientCreator}'s I{connect} methods and assert that all of its
arguments except for the factory are passed on as expected to the
reactor. The factory should be returned.
"""
class SomeProtocol(Protocol):
pass
reactor = MemoryReactorWithConnectorsAndTime()
cc = ClientCreator(reactor, SomeProtocol)
factory = check(reactor, cc)
protocol = factory.buildProtocol(None)
self.assertIsInstance(protocol, SomeProtocol)
0
Example 29
Project: mythbox Source File: test_protocol.py
def _cancelConnectTest(self, connect):
"""
Helper for implementing a test to verify that cancellation of the
L{Deferred} returned by one of L{ClientCreator}'s I{connect} methods is
implemented to cancel the underlying connector.
@param connect: A function which will be invoked with a L{ClientCreator}
instance as an argument and which should call one its I{connect}
methods and return the result.
@return: A L{Deferred} which fires when the test is complete or fails if
there is a problem.
"""
reactor = MemoryReactorWithConnectorsAndTime()
cc = ClientCreator(reactor, Protocol)
d = connect(cc)
connector = reactor.connectors.pop()
self.assertFalse(connector._disconnected)
d.cancel()
self.assertTrue(connector._disconnected)
return self.assertFailure(d, CancelledError)
0
Example 30
def _cancelConnectTimeoutTest(self, connect):
"""
Like L{_cancelConnectTest}, but for the case where the L{Deferred} is
cancelled after the connection is set up but before it is fired with the
resulting protocol instance.
"""
reactor = MemoryReactorWithConnectorsAndTime()
cc = ClientCreator(reactor, Protocol)
d = connect(reactor, cc)
connector = reactor.connectors.pop()
# Sanity check - there is an outstanding delayed call to fire the
# Deferred.
self.assertEquals(len(reactor.getDelayedCalls()), 1)
# Cancel the Deferred, disconnecting the transport just set up and
# cancelling the delayed call.
d.cancel()
self.assertEquals(reactor.getDelayedCalls(), [])
# A real connector implementation is responsible for disconnecting the
# transport as well. For our purposes, just check that someone told the
# connector to disconnect.
self.assertTrue(connector._disconnected)
return self.assertFailure(d, CancelledError)
0
Example 31
def _cancelConnectFailedTimeoutTest(self, connect):
"""
Like L{_cancelConnectTest}, but for the case where the L{Deferred} is
cancelled after the connection attempt has failed but before it is fired
with the resulting failure.
"""
reactor = MemoryReactorWithConnectorsAndTime()
cc = ClientCreator(reactor, Protocol)
d, factory = connect(reactor, cc)
connector = reactor.connectors.pop()
factory.clientConnectionFailed(
connector, Failure(Exception("Simulated failure")))
# Sanity check - there is an outstanding delayed call to fire the
# Deferred.
self.assertEquals(len(reactor.getDelayedCalls()), 1)
# Cancel the Deferred, cancelling the delayed call.
d.cancel()
self.assertEquals(reactor.getDelayedCalls(), [])
return self.assertFailure(d, CancelledError)
0
Example 32
Project: ccs-calendarserver Source File: agent.py
def getList():
# For the sample client, below:
from twisted.internet import reactor
from twisted.internet.protocol import ClientCreator
creator = ClientCreator(reactor, amp.AMP)
host = '127.0.0.1'
import sys
if len(sys.argv) > 1:
host = sys.argv[1]
d = creator.connectTCP(host, 62308)
def connected(ampProto):
return ampProto.callRemote(GatewayAMPCommand, command=command)
d.addCallback(connected)
def resulted(result):
return result['result']
d.addCallback(resulted)
def done(result):
print('Done: %s' % (result,))
reactor.stop()
d.addCallback(done)
reactor.run()
0
Example 33
Project: pgproxy Source File: proxy.py
def makePostgresProtocol(self):
"""
Creates the postgres server connection. This is called only if
self.postgresProtocol is not set.
"""
if self.creatingPostgresProtocol:
log.msg('Already creating postgres protocol')
return self.creatingPostgresProtocol
log.msg('Creating Postgres connection.')
def gotProto(p):
if p.dead:
log.msg('PostgresClientProtocol died immediately.')
return
if self.postgresProtocol:
if self.postgresProtocol.dead:
log.msg('Already had postgres protocol, but it was dead.')
else:
log.msg('Already had postgres protocol')
return self.postgresProtocol
log.msg('Got PostgresClientProtocol instance.')
self.postgresProtocol = p
self.creatingPostgresProtocol = None
return p
cc = protocol.ClientCreator(reactor, PostgresClientProtocol)
self.creatingPostgresProtocol = cc.connectTCP(
self.pgproxy.config['server-host'],
self.pgproxy.config['server-port']).addCallback(gotProto)
return self.creatingPostgresProtocol
0
Example 34
def _cancelConnectTimeoutTest(self, connect):
"""
Like L{_cancelConnectTest}, but for the case where the L{Deferred} is
cancelled after the connection is set up but before it is fired with the
resulting protocol instance.
"""
reactor = MemoryReactorWithConnectorsAndTime()
cc = ClientCreator(reactor, Protocol)
d = connect(reactor, cc)
connector = reactor.connectors.pop()
# Sanity check - there is an outstanding delayed call to fire the
# Deferred.
self.assertEqual(len(reactor.getDelayedCalls()), 1)
# Cancel the Deferred, disconnecting the transport just set up and
# cancelling the delayed call.
d.cancel()
self.assertEqual(reactor.getDelayedCalls(), [])
# A real connector implementation is responsible for disconnecting the
# transport as well. For our purposes, just check that someone told the
# connector to disconnect.
self.assertTrue(connector._disconnected)
return self.assertFailure(d, CancelledError)
0
Example 35
Project: SubliminalCollaborator Source File: test_protocol.py
Function: cancelconnectfailedtimeouttest
Function: cancelconnectfailedtimeouttest
def _cancelConnectFailedTimeoutTest(self, connect):
"""
Like L{_cancelConnectTest}, but for the case where the L{Deferred} is
cancelled after the connection attempt has failed but before it is fired
with the resulting failure.
"""
reactor = MemoryReactorWithConnectorsAndTime()
cc = ClientCreator(reactor, Protocol)
d, factory = connect(reactor, cc)
connector = reactor.connectors.pop()
factory.clientConnectionFailed(
connector, Failure(Exception("Simulated failure")))
# Sanity check - there is an outstanding delayed call to fire the
# Deferred.
self.assertEqual(len(reactor.getDelayedCalls()), 1)
# Cancel the Deferred, cancelling the delayed call.
d.cancel()
self.assertEqual(reactor.getDelayedCalls(), [])
return self.assertFailure(d, CancelledError)
0
Example 36
Project: SubliminalCollaborator Source File: oscar.py
def connectToBOS(self, server, port):
c = protocol.ClientCreator(reactor, self.BOSClass, self.username, self.cookie)
return c.connectTCP(server, int(port))
0
Example 37
Project: gitmouth Source File: session.py
def execCommand(self, proto, cmd):
proto.makeConnection(DumbProtocol())
def return_path_error():
proto.errReceived('\n ! Invalid path.')
proto.errReceived('\n ! Syntax is: [email protected]:<app>.git where '
'<app> is your app\'s name.\n\n')
reason = failure.Failure(ProcessTerminated(exitCode=1))
return proto.processEnded(reason)
if not cmd:
return return_path_error()
cmd_parts = cmd.split(' ')
if len(cmd_parts) != 2:
return return_path_error()
app_command = cmd_parts[0]
if app_command not in ['git-receive-pack', 'git-upload-pack']:
return return_path_error()
app_name_raw = cmd_parts[1]
name_match = self.app_name_regex.match(app_name_raw)
if name_match is None:
return return_path_error()
app_name = name_match.group('app_name')
def buildProtoCallback(dyno_id):
def setupProto(remoteProto):
self.rez = remoteProto
remoteProto.write(self.settings['apiserver_key'] + '\n')
remoteProto.write(dyno_id + '\n')
proto.makeConnection(remoteProto)
remoteProto.pair(proto)
return setupProto
def connect_to_rez(rez_info):
dyno_id = rez_info.get('dyno_id')
cc = ClientCreator(reactor, ProcLiteProtocol)
(cc.connectSSL(rez_info.get('host'),
self.settings['dynohost_rendezvous_port'],
ssl.ClientContextFactory()).
addCallback(buildProtoCallback(dyno_id)))
d = defer.Deferred()
d.addCallback(connect_to_rez)
def cbErrResponse(err):
proto.errReceived('\n ! Unable to contact build server.\n\n')
reason = failure.Failure(ProcessTerminated(exitCode=127))
return proto.processEnded(reason)
def cbResponse(resp):
if resp.code == 200:
resp.deliverBody(BuildServerExtractor(d))
else:
proto.errReceived('\n ! Unable to contact build server.\n\n')
reason = failure.Failure(ProcessTerminated(exitCode=127))
return proto.processEnded(reason)
url = '%s://%s:%d/internal/%s/gitaction?command=%s' % (
self.settings['apiserver_protocol'],
self.settings['apiserver_hostname'],
self.settings['apiserver_port'],
app_name,
app_command
)
auth_key = base64.b64encode(':%s' % self.settings['apiserver_key'])
headers = Headers({'Authorization': [' Basic %s' % auth_key]})
req = self.agent.request('POST', url, headers=headers)
req.addCallback(cbResponse)
req.addErrback(cbErrResponse)
return
0
Example 38
Project: droned Source File: __init__.py
def connect(host, port, protocol, *proto_args, **proto_kwargs):
"""connect(host, port, protocol, *proto_args, **proto_kwargs)
Initiate a TCP connection to the given host and port using the given
protocol class. *proto_args and **proto_kwargs are passed to the protocol
constructor, and the last positional argument will be a Deferred for the
result of the task. The protocol constructor must take at least this one
argument.
"""
deferredResult = defer.Deferred()
proto_args += (deferredResult,)
if 'timeout' in proto_kwargs:
timeout = proto_kwargs.pop('timeout')
else:
timeout = None
try: #dmx work around
import config
reactor = config.reactor
except:
from twisted.internet import reactor
connector = ClientCreator(reactor, protocol, *proto_args, **proto_kwargs)
deferredConnect = connector.connectTCP(host, port)
from kitt.decorators import debugCall
if 'debug' in proto_kwargs and proto_kwargs['debug'] == True:
deferredResult.errback = debugCall( deferredResult.errback )
deferredResult.callback = debugCall( deferredResult.callback )
#If the connection fails the protocol task fails
deferredConnect.addErrback(lambda failure: deferredResult.called or
deferredResult.errback(failure))
if timeout:
reactor.callLater(timeout, cancelTask, deferredResult)
#Inject the server name and port into the results in the callback chain
def injectServer(outcome):
if isinstance(outcome, dict):
outcome['server'] = proto_kwargs.get('hostname',host)
outcome['port'] = port
elif isinstance(outcome, Failure) and outcome.check(DroneCommandFailed):
outcome.value.resultContext['server'] = proto_kwargs.get('hostname',
host)
outcome.value.resultContext['port'] = port
return outcome
if 'debug' in proto_kwargs and proto_kwargs['debug'] == True:
injectServer = debugCall( injectServer )
deferredResult.addBoth(injectServer)
return deferredResult