1 # -*- test-case-name: twisted.test.test_strports -*-
2 # Copyright (c) Twisted Matrix Laboratories.
3 # See LICENSE for details.
6 Construct listening port services from a simple string description.
8 @see: L{twisted.internet.endpoints.serverFromString}
9 @see: L{twisted.internet.endpoints.clientFromString}
14 from twisted.internet import endpoints
15 from twisted.python.deprecate import deprecatedModuleAttribute
16 from twisted.python.versions import Version
17 from twisted.application.internet import StreamServerEndpointService
21 def parse(description, factory, default='tcp'):
23 This function is deprecated as of Twisted 10.2.
25 @see: L{twisted.internet.endpoints.server}
27 return endpoints._parseServer(description, factory, default)
29 deprecatedModuleAttribute(
30 Version("Twisted", 10, 2, 0),
31 "in favor of twisted.internet.endpoints.serverFromString",
38 def service(description, factory, default=_DEFAULT, reactor=None):
40 Return the service corresponding to a description.
42 @param description: The description of the listening port, in the syntax
43 described by L{twisted.internet.endpoints.server}.
45 @type description: C{str}
47 @param factory: The protocol factory which will build protocols for
48 connections to this service.
50 @type factory: L{twisted.internet.interfaces.IProtocolFactory}
52 @type default: C{str} or C{None}
54 @param default: Do not use this parameter. It has been deprecated since
57 @rtype: C{twisted.application.service.IService}
59 @return: the service corresponding to a description of a reliable
62 @see: L{twisted.internet.endpoints.serverFromString}
65 from twisted.internet import reactor
66 if default is _DEFAULT:
69 message = "The 'default' parameter was deprecated in Twisted 10.2.0."
70 if default is not None:
72 " Use qualified endpoint descriptions; for example, "
73 "'tcp:%s'." % (description,))
75 message=message, category=DeprecationWarning, stacklevel=2)
76 svc = StreamServerEndpointService(
77 endpoints._serverFromStringLegacy(reactor, description, default),
79 svc._raiseSynchronously = True
84 def listen(description, factory, default=None):
85 """Listen on a port corresponding to a description
87 @type description: C{str}
88 @type factory: L{twisted.internet.interfaces.IProtocolFactory}
89 @type default: C{str} or C{None}
90 @rtype: C{twisted.internet.interfaces.IListeningPort}
91 @return: the port corresponding to a description of a reliable
92 virtual circuit server.
94 See the documentation of the C{parse} function for description
95 of the semantics of the arguments.
97 from twisted.internet import reactor
98 name, args, kw = parse(description, factory, default)
99 return getattr(reactor, 'listen'+name)(*args, **kw)
103 __all__ = ['parse', 'service', 'listen']