You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

process_twisted.py 1.2KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. """A process that reads from stdin and out using Twisted."""
  2. from __future__ import division, absolute_import, print_function
  3. ### Twisted Preamble
  4. # This makes sure that users don't have to set up their environment
  5. # specially in order to run these programs from bin/.
  6. import sys, os
  7. pos = os.path.abspath(sys.argv[0]).find(os.sep+'Twisted')
  8. if pos != -1:
  9. sys.path.insert(0, os.path.abspath(sys.argv[0])[:pos+8])
  10. sys.path.insert(0, os.curdir)
  11. ### end of preamble
  12. from twisted.python import log
  13. from zope.interface import implementer
  14. from twisted.internet import interfaces
  15. log.startLogging(sys.stderr)
  16. from twisted.internet import protocol, reactor, stdio
  17. @implementer(interfaces.IHalfCloseableProtocol)
  18. class Echo(protocol.Protocol):
  19. def connectionMade(self):
  20. print("connection made")
  21. def dataReceived(self, data):
  22. self.transport.write(data)
  23. def readConnectionLost(self):
  24. print("readConnectionLost")
  25. self.transport.loseConnection()
  26. def writeConnectionLost(self):
  27. print("writeConnectionLost")
  28. def connectionLost(self, reason):
  29. print("connectionLost", reason)
  30. reactor.stop()
  31. stdio.StandardIO(Echo())
  32. reactor.run()