strports.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # -*- test-case-name: twisted.test.test_strports -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Construct listening port services from a simple string description.
  6. @see: L{twisted.internet.endpoints.serverFromString}
  7. @see: L{twisted.internet.endpoints.clientFromString}
  8. """
  9. from __future__ import absolute_import, division
  10. from twisted.application.internet import StreamServerEndpointService
  11. from twisted.internet import endpoints
  12. def service(description, factory, reactor=None):
  13. """
  14. Return the service corresponding to a description.
  15. @param description: The description of the listening port, in the syntax
  16. described by L{twisted.internet.endpoints.serverFromString}.
  17. @type description: C{str}
  18. @param factory: The protocol factory which will build protocols for
  19. connections to this service.
  20. @type factory: L{twisted.internet.interfaces.IProtocolFactory}
  21. @rtype: C{twisted.application.service.IService}
  22. @return: the service corresponding to a description of a reliable stream
  23. server.
  24. @see: L{twisted.internet.endpoints.serverFromString}
  25. """
  26. if reactor is None:
  27. from twisted.internet import reactor
  28. svc = StreamServerEndpointService(
  29. endpoints.serverFromString(reactor, description), factory)
  30. svc._raiseSynchronously = True
  31. return svc
  32. def listen(description, factory):
  33. """
  34. Listen on a port corresponding to a description.
  35. @param description: The description of the connecting port, in the syntax
  36. described by L{twisted.internet.endpoints.serverFromString}.
  37. @type description: L{str}
  38. @param factory: The protocol factory which will build protocols on
  39. connection.
  40. @type factory: L{twisted.internet.interfaces.IProtocolFactory}
  41. @rtype: L{twisted.internet.interfaces.IListeningPort}
  42. @return: the port corresponding to a description of a reliable virtual
  43. circuit server.
  44. @see: L{twisted.internet.endpoints.serverFromString}
  45. """
  46. from twisted.internet import reactor
  47. name, args, kw = endpoints._parseServer(description, factory)
  48. return getattr(reactor, 'listen' + name)(*args, **kw)
  49. __all__ = ['service', 'listen']