Exemplo n.º 1
0
def clientFromString(reactor, description):
	"""
	Construct a client endpoint from a description string.

	Client description strings are much like server description strings,
	although they take all of their arguments as keywords, since even the
	simplest client endpoint (plain TCP) requires at least 2 arguments (host
	and port) to construct.

	You can create a TCP client endpoint with the 'host' and 'port' arguments,
	like so::

		clientFromString(reactor, "tcp:host=www.example.com:port=80")

	or an SSL client endpoint with those arguments, plus the arguments used by
	the server SSL, for a client certificate::

		clientFromString(reactor, "ssl:host=web.example.com:port=443:"
								  "privateKey=foo.pem:certKey=foo.pem")

	to specify your certificate trust roots, you can identify a directory with
	PEM files in it with the C{caCertsDir} argument::

		clientFromString(reactor, "ssl:host=web.example.com:port=443:"
								  "caCertsDir=/etc/ssl/certs")

	This function is also extensible; new endpoint types may be registered as
	L{IStreamClientEndpointStringParser} plugins.  See that interface for more
	information.

	@param reactor: The client endpoint will be constructed with this reactor.

	@param description: The strports description to parse.

	@return: A new endpoint which can be used to connect with the parameters
		given by by C{description}.
	@rtype: L{IStreamClientEndpoint<lib.twisted.internet.interfaces.IStreamClientEndpoint>}

	@since: 10.2
	"""
	args, kwargs = _parse(description)
	aname = args.pop(0)
	name = aname.upper()
	for plugin in getPlugins(IStreamClientEndpointStringParser):
		if plugin.prefix.upper() == name:
			return plugin.parseStreamClient(*args, **kwargs)
	if name not in _clientParsers:
		raise ValueError("Unknown endpoint type: %r" % (aname,))
	kwargs = _clientParsers[name](*args, **kwargs)
	return _endpointClientFactories[name](reactor, **kwargs)
Exemplo n.º 2
0
def _parseServer(description, factory, default=None):
	"""
	Parse a stports description into a 2-tuple of arguments and keyword values.

	@param description: A description in the format explained by
		L{serverFromString}.
	@type description: C{str}

	@param factory: A 'factory' argument; this is left-over from
		lib.twisted.application.strports, it's not really used.
	@type factory: L{IProtocolFactory} or L{None}

	@param default: Deprecated argument, specifying the default parser mode to
		use for unqualified description strings (those which do not have a ':'
		and prefix).
	@type default: C{str} or C{NoneType}

	@return: a 3-tuple of (plugin or name, arguments, keyword arguments)
	"""
	args, kw = _parse(description)
	if not args or (len(args) == 1 and not kw):
		deprecationMessage = (
			"Unqualified strport description passed to 'service'."
			"Use qualified endpoint descriptions; for example, 'tcp:%s'."
			% (description,))
		if default is None:
			default = 'tcp'
			warnings.warn(
				deprecationMessage, category=DeprecationWarning, stacklevel=4)
		elif default is _NO_DEFAULT:
			raise ValueError(deprecationMessage)
		# If the default has been otherwise specified, the user has already
		# been warned.
		args[0:0] = [default]
	endpointType = args[0]
	parser = _serverParsers.get(endpointType)
	if parser is None:
		for plugin in getPlugins(IStreamServerEndpointStringParser):
			if plugin.prefix == endpointType: 
				return (plugin, args[1:], kw)
		raise ValueError("Unknown endpoint type: '%s'" % (endpointType,))
	return (endpointType.upper(),) + parser(factory, *args[1:], **kw)