def connect(self, protocol_factory):
"""
Connect to the C{protocolFactory} to the AMQP broker specified by the
URI of this endpoint.
@param protocol_factory: An L{AMQFactory} building L{AMQClient} objects.
@return: A L{Deferred} that results in an L{AMQClient} upon successful
connection otherwise a L{Failure} wrapping L{ConnectError} or
L{NoProtocol <twisted.internet.error.NoProtocol>}.
"""
# XXX Since AMQClient requires these parameters at __init__ time, we
# need to override them in the provided factory.
protocol_factory.set_vhost(self._vhost)
protocol_factory.set_heartbeat(self._heartbeat)
description = "tcp:{}:{}:timeout={}".format(
self._host, self._port, self._timeout)
endpoint = clientFromString(self._reactor, description)
deferred = endpoint.connect(protocol_factory)
return deferred.addCallback(self._authenticate)
python类clientFromString()的实例源码
def do_build_transport(self):
if len(self.tor_control_unix_socket) == 0:
assert len(self.onion_tcp_interface_ip) != 0
tor_controller_endpoint_desc = "tcp:%s:%s" % (self.tor_control_tcp_host, self.tor_control_tcp_port)
else:
tor_controller_endpoint_desc = "unix:%s" % self.tor_control_unix_socket
tor_controller_endpoint = endpoints.clientFromString(self.reactor, tor_controller_endpoint_desc)
tor = yield txtorcon.connect(self.reactor, control_endpoint=tor_controller_endpoint)
onion_tcp_port = 0
if len(self.onion_unix_socket) == 0:
onion_tcp_port = yield txtorcon.util.available_tcp_port(self.reactor)
hs = txtorcon.EphemeralHiddenService(["%s %s:%s" % (self.onion_service_port, self.onion_tcp_interface_ip, onion_tcp_port)])
else:
hs = txtorcon.EphemeralHiddenService(["%s unix:%s" % (self.onion_service_port, self.onion_unix_socket)])
yield hs.add_to_tor(tor.protocol)
transport = OnionTransport(self.reactor,
tor,
onion_host=hs.hostname.encode('utf-8'),
onion_port=self.onion_service_port,
onion_key=hs.private_key.encode('utf-8'),
onion_tcp_interface_ip=self.onion_tcp_interface_ip,
onion_tcp_port=onion_tcp_port)
yield hs.remove_from_tor(tor.protocol)
defer.returnValue(transport)
def connectionMade(self):
logger.info('[%s] Connection received from VNC client', self.id)
factory = protocol.ClientFactory()
factory.protocol = VNCProxyClient
factory.vnc_server = self
factory.deferrable = defer.Deferred()
endpoint = endpoints.clientFromString(reactor, self.factory.vnc_address)
def _established_callback(client):
if self._broken:
client.close()
self.vnc_client = client
self.flush()
def _established_errback(reason):
logger.error('[VNCProxyServer] Connection succeeded but could not establish session: %s', reason)
self.close()
factory.deferrable.addCallbacks(_established_callback, _established_errback)
def _connect_errback(reason):
logger.error('[VNCProxyServer] Connection failed: %s', reason)
self.close()
endpoint.connect(factory).addErrback(_connect_errback)
self.send_ProtocolVersion_Handshake()
def test_tcp(self):
"""
When passed a TCP strports description, L{endpoints.clientFromString}
returns a L{TCP4ClientEndpoint} instance initialized with the values
from the string.
"""
reactor = object()
client = endpoints.clientFromString(
reactor,
"tcp:host=example.com:port=1234:timeout=7:bindAddress=10.0.0.2")
self.assertIsInstance(client, endpoints.TCP4ClientEndpoint)
self.assertIs(client._reactor, reactor)
self.assertEqual(client._host, "example.com")
self.assertEqual(client._port, 1234)
self.assertEqual(client._timeout, 7)
self.assertEqual(client._bindAddress, ("10.0.0.2", 0))
def test_tcpPositionalArgs(self):
"""
When passed a TCP strports description using positional arguments,
L{endpoints.clientFromString} returns a L{TCP4ClientEndpoint} instance
initialized with the values from the string.
"""
reactor = object()
client = endpoints.clientFromString(
reactor,
"tcp:example.com:1234:timeout=7:bindAddress=10.0.0.2")
self.assertIsInstance(client, endpoints.TCP4ClientEndpoint)
self.assertIs(client._reactor, reactor)
self.assertEqual(client._host, "example.com")
self.assertEqual(client._port, 1234)
self.assertEqual(client._timeout, 7)
self.assertEqual(client._bindAddress, ("10.0.0.2", 0))
def test_stringParserWithReactor(self):
"""
L{endpoints.clientFromString} will pass a reactor to plugins
implementing the L{IStreamClientEndpointStringParserWithReactor}
interface.
"""
addFakePlugin(self)
reactor = object()
clientEndpoint = endpoints.clientFromString(
reactor, 'crfake:alpha:beta:cee=dee:num=1')
from twisted.plugins.fakeendpoint import fakeClientWithReactor
self.assertEqual(
(clientEndpoint.parser,
clientEndpoint.args,
clientEndpoint.kwargs),
(fakeClientWithReactor,
(reactor, 'alpha', 'beta'),
dict(cee='dee', num='1')))
def test_sslPositionalArgs(self):
"""
When passed an SSL strports description, L{clientFromString} returns a
L{SSL4ClientEndpoint} instance initialized with the values from the
string.
"""
reactor = object()
client = endpoints.clientFromString(
reactor,
"ssl:example.net:4321:privateKey=%s:"
"certKey=%s:bindAddress=10.0.0.3:timeout=3:caCertsDir=%s" %
(escapedPEMPathName, escapedPEMPathName, escapedCAsPathName))
self.assertIsInstance(client, endpoints.SSL4ClientEndpoint)
self.assertIs(client._reactor, reactor)
self.assertEqual(client._host, "example.net")
self.assertEqual(client._port, 4321)
self.assertEqual(client._timeout, 3)
self.assertEqual(client._bindAddress, ("10.0.0.3", 0))
def test_sslWithDefaults(self):
"""
When passed an SSL strports description without extra arguments,
L{clientFromString} returns a L{SSL4ClientEndpoint} instance
whose context factory is initialized with default values.
"""
reactor = object()
client = endpoints.clientFromString(reactor, "ssl:example.net:4321")
self.assertIsInstance(client, endpoints.SSL4ClientEndpoint)
self.assertIs(client._reactor, reactor)
self.assertEqual(client._host, "example.net")
self.assertEqual(client._port, 4321)
certOptions = client._sslContextFactory
self.assertEqual(certOptions.method, SSLv23_METHOD)
self.assertIsNone(certOptions.certificate)
self.assertIsNone(certOptions.privateKey)
def test_hostnameEndpointConstruction(self):
"""
A L{HostnameEndpoint} is constructed from parameters passed to
L{clientFromString}.
"""
reactor = object()
endpoint = endpoints.clientFromString(
reactor,
nativeString(
'tls:example.com:443:timeout=10:bindAddress=127.0.0.1'))
hostnameEndpoint = endpoint._wrappedEndpoint
self.assertIs(hostnameEndpoint._reactor, reactor)
self.assertEqual(hostnameEndpoint._hostBytes, b'example.com')
self.assertEqual(hostnameEndpoint._port, 443)
self.assertEqual(hostnameEndpoint._timeout, 10)
self.assertEqual(hostnameEndpoint._bindAddress,
nativeString('127.0.0.1'))
def test_utf8Encoding(self):
"""
The hostname passed to L{clientFromString} is treated as utf-8 bytes;
it is then encoded as IDNA when it is passed along to
L{HostnameEndpoint}, and passed as unicode to L{optionsForClientTLS}.
"""
reactor = object()
endpoint = endpoints.clientFromString(
reactor, b'tls:\xc3\xa9xample.example.com:443'
)
self.assertEqual(
endpoint._wrappedEndpoint._hostBytes,
b'xn--xample-9ua.example.com'
)
connectionCreator = connectionCreatorFromEndpoint(
reactor, endpoint)
self.assertEqual(connectionCreator._hostname,
u'\xe9xample.example.com')
def test_onion_datagram_proxy():
received_buffer = []
received_d = defer.Deferred()
def received(data):
received_buffer.append(data)
received_d.callback(None)
received_size = 10
proxy_factory = OnionDatagramProxyFactory(received)
protocol = proxy_factory.buildProtocol(123)
packet = b"A" * received_size
protocol.stringReceived(packet)
assert received_buffer[0] == packet
service_port = yield txtorcon.util.available_tcp_port(reactor)
service_endpoint_desc = "tcp:interface=127.0.0.1:%s" % service_port
service_endpoint = endpoints.serverFromString(reactor, service_endpoint_desc)
yield service_endpoint.listen(proxy_factory)
client_endpoint_desc = "tcp:127.0.0.1:%s" % service_port
client_endpoint = endpoints.clientFromString(reactor, client_endpoint_desc)
client_protocol = Int32StringReceiver()
yield endpoints.connectProtocol(client_endpoint, client_protocol)
client_protocol.sendString(packet)
print "BEFORE CLOSE"
client_protocol.transport.loseConnection()
yield received_d
assert received_buffer[0] == packet
def __init__(self, vpnconfig, providerconfig, socket_host, socket_port,
openvpn_verb, remotes, restartfun=None):
"""
:param vpnconfig: vpn configuration object
:type vpnconfig: VPNConfig
:param providerconfig: provider specific configuration
:type providerconfig: ProviderConfig
:param socket_host: either socket path (unix) or socket IP
:type socket_host: str
:param socket_port: either string "unix" if it's a unix
socket, or port otherwise
:type socket_port: str
"""
self._host = socket_host
self._port = socket_port
if socket_port == 'unix':
folder = os.path.split(self._host)[0]
if not os.path.isdir(folder):
os.makedirs(folder)
self._management_endpoint = clientFromString(
reactor, b"unix:path=%s" % socket_host)
else:
raise ValueError('tcp endpoint not configured')
self._vpnconfig = vpnconfig
self._providerconfig = providerconfig
self._launcher = get_vpn_launcher()
self._restartfun = restartfun
self.restarting = True
self.failed = False
self.errmsg = None
self.proto = None
self._remotes = remotes
self._statelog = OrderedDict()
self._turn_state_off()
def connect_upstream(self, tries=1, max_attempts=7):
if self._closed:
logger.info("[RewardProxyServer] [%d] Attempted to connect upstream although client connection is already closed. Aborting",
self.id)
return
remote = getattr(self.factory, 'rewarder_address', 'localhost:15900')
endpoint = endpoints.clientFromString(reactor, 'tcp:' + remote)
client_factory = websocket.WebSocketClientFactory('ws://' + remote)
headers = {'authorization': self._request.headers['authorization']}
if self._request.headers.get('openai-observer'):
headers['openai-observer'] = self._request.headers.get('openai-observer')
client_factory.headers = headers
client_factory.protocol = RewardServerClient
client_factory.proxy_server = self
client_factory.endpoint = endpoint
logger.info("[RewardProxyServer] [%d] Connecting to upstream %s (try %d/%d)", self.id, remote, tries, max_attempts)
def _connect_callback(client):
logger.info('[RewardProxyServer] [%d] Upstream connection %s established', self.id, remote)
self.client = client
if self.factory.logfile_dir:
self.begin_recording()
def _connect_errback(reason):
if tries < max_attempts:
# Somewhat arbitrary exponential backoff: should be
# pretty rare, and indicate that we're just starting
# up.
delay = 1.5 ** tries
logger.info('[RewardProxyServer] [%d] Connection to %s failed: %s. Try %d/%d; going to retry in %fs', self.id, remote, reason, tries, max_attempts, delay)
reactor.callLater(
delay, self.connect_upstream,
tries=tries+1, max_attempts=max_attempts)
else:
logger.error('[RewardProxyServer] [%d] Connection to %s failed: %s. Completed %d/%d atttempts; disconnecting.', self.id, remote, reason, tries, max_attempts)
self.transport.loseConnection()
endpoint.connect(client_factory).addCallbacks(_connect_callback, _connect_errback)
def _connect(self):
deferreds = []
for i, remote in enumerate(self.remotes):
d = defer.Deferred()
deferreds.append(d)
factory = vnc_client.client_factory(d, self.error_buffer)
factory.rewarder_session = self
factory.label = 'vnc:{}:{}'.format(i, remote)
endpoint = endpoints.clientFromString(reactor, 'tcp:'+remote)
def success(i):
logger.info('[%s] VNC connection established', factory.label)
def fail(reason):
reason = error.Error('[{}] Connection failed: {}'.format(factory.label, reason.value))
try:
d.errback(utils.format_error(reason))
except defer.AlreadyCalledError:
pass
endpoint.connect(factory).addCallback(success).addErrback(fail)
d = defer.DeferredList(deferreds, fireOnOneErrback=True)
def success(results):
# Store the _clients list when connected
self._clients = [client for success, client in results]
d.addCallback(success)
return d
def SetupConnection(self, host, port):
logger.debug("Setting up connection! %s %s " % (host, port))
factory = Factory.forProtocol(NeoNode)
endpoint = clientFromString(reactor, "tcp:host=%s:port=%s:timeout=5" % (host, port))
connectingService = ClientService(
endpoint,
factory,
retryPolicy=backoffPolicy(.5, factor=3.0)
)
connectingService.startService()
def __init__(self, request, config):
self.request = request
self.config = config
factory = LDAPFactory()
endpoint = clientFromString(reactor, self.config['client'])
d = endpoint.connect(factory)
d.addCallback(self.gotConnection)
def connectionMade(self):
self.log.debug("EndpointForwardingProtocol.connectionMade")
self._destFactory = DestEndpointForwardingFactory(self)
self._destEndpoint = clientFromString(self.factory.service._reactor,
self.factory.service._destEndpointDescriptor)
self._destEndpointPort = yield self._destEndpoint.connect(self._destFactory)
def parseStreamClient(self, *args, **options):
if _HAS_REACTOR_ARG:
reactor = args[0]
if len(args) != 2:
raise RuntimeError("autobahn: client plugin takes exactly one positional argument")
description = args[1]
else:
from twisted.internet import reactor
if len(args) != 1:
raise RuntimeError("autobahn: client plugin takes exactly one positional argument")
description = args[0]
opts = _parseOptions(options)
endpoint = clientFromString(reactor, description)
return AutobahnClientEndpoint(reactor, endpoint, opts)
def test_tcpHostPositionalArg(self):
"""
When passed a TCP strports description specifying host as a positional
argument, L{endpoints.clientFromString} returns a L{TCP4ClientEndpoint}
instance initialized with the values from the string.
"""
reactor = object()
client = endpoints.clientFromString(
reactor,
"tcp:example.com:port=1234:timeout=7:bindAddress=10.0.0.2")
self.assertEqual(client._host, "example.com")
self.assertEqual(client._port, 1234)
def test_tcpPortPositionalArg(self):
"""
When passed a TCP strports description specifying port as a positional
argument, L{endpoints.clientFromString} returns a L{TCP4ClientEndpoint}
instance initialized with the values from the string.
"""
reactor = object()
client = endpoints.clientFromString(
reactor,
"tcp:host=example.com:1234:timeout=7:bindAddress=10.0.0.2")
self.assertEqual(client._host, "example.com")
self.assertEqual(client._port, 1234)
def test_tcpDefaults(self):
"""
A TCP strports description may omit I{timeout} or I{bindAddress} to
allow the default to be used.
"""
reactor = object()
client = endpoints.clientFromString(
reactor,
"tcp:host=example.com:port=1234")
self.assertEqual(client._timeout, 30)
self.assertIsNone(client._bindAddress)
def test_unixDefaults(self):
"""
A UNIX strports description may omit I{lockfile} or I{timeout} to allow
the defaults to be used.
"""
client = endpoints.clientFromString(
object(), "unix:path=/var/foo/bar")
self.assertEqual(client._timeout, 30)
self.assertFalse(client._checkPID)
def test_unixPathPositionalArg(self):
"""
When passed a UNIX strports description specifying path as a positional
argument, L{endpoints.clientFromString} returns a L{UNIXClientEndpoint}
instance initialized with the values from the string.
"""
reactor = object()
client = endpoints.clientFromString(
reactor,
"unix:/var/foo/bar:lockfile=1:timeout=9")
self.assertIsInstance(client, endpoints.UNIXClientEndpoint)
self.assertIs(client._reactor, reactor)
self.assertEqual(client._path, "/var/foo/bar")
self.assertEqual(client._timeout, 9)
self.assertTrue(client._checkPID)
def test_typeFromPlugin(self):
"""
L{endpoints.clientFromString} looks up plugins of type
L{IStreamClientEndpoint} and constructs endpoints from them.
"""
addFakePlugin(self)
notAReactor = object()
clientEndpoint = endpoints.clientFromString(
notAReactor, "crfake:alpha:beta:cee=dee:num=1")
from twisted.plugins.fakeendpoint import fakeClientWithReactor
self.assertIs(clientEndpoint.parser, fakeClientWithReactor)
self.assertEqual(clientEndpoint.args, (notAReactor, 'alpha', 'beta'))
self.assertEqual(clientEndpoint.kwargs, dict(cee='dee', num='1'))
def test_unknownType(self):
"""
L{endpoints.clientFromString} raises C{ValueError} when given an
unknown endpoint type.
"""
value = self.assertRaises(
# faster-than-light communication not supported
ValueError, endpoints.clientFromString, None,
"ftl:andromeda/carcosa/hali/2387")
self.assertEqual(
str(value),
"Unknown endpoint type: 'ftl'")
def test_sslSimple(self):
"""
When passed an SSL strports description without any extra parameters,
L{clientFromString} returns a simple non-verifying endpoint that will
speak SSL.
"""
reactor = object()
client = endpoints.clientFromString(
reactor, "ssl:host=simple.example.org:port=4321")
certOptions = client._sslContextFactory
self.assertIsInstance(certOptions, CertificateOptions)
self.assertFalse(certOptions.verify)
ctx = certOptions.getContext()
self.assertIsInstance(ctx, ContextType)
def _getEndpoint(self, *_a):
return clientFromString(reactor, self.path)
def main(reactor, procName, *args):
procName = os.path.basename(procName)
clientEndpoints = {}
for k, v in os.environ.iteritems():
_, _, clientName = k.partition('client_endpoint_')
if clientName:
clientEndpoints[clientName] = clientFromString(reactor, v)
if not clientEndpoints:
raise ValueError("no client endpoints detected in the environment")
plugins = [pluginClass(clientEndpoints)
for pluginClass in sorted(pluginClasses, key=nameLength, reverse=True)]
if args == ('autoconf',):
print 'yes'
return defer.succeed(None)
if args == ('suggest',):
suggestions = []
for plugin in plugins:
suggestions.extend((plugin.name + arg).partition(procName)[2]
for arg in plugin.suggest())
print '\n'.join(suggestion for suggestion in suggestions if suggestion)
return defer.succeed(None)
for plugin in plugins:
_, foundPluginName, arg = procName.partition(plugin.name)
if not foundPluginName:
continue
command = 'fetch' if not args else args[0]
method = getattr(plugin, 'command_' + command, None)
if not method:
raise ValueError("%r plugin can't handle the command %r" % (plugin.name, command))
return defer.maybeDeferred(method, arg)
raise ValueError("no plugin was found with the name %r" % (procName,))