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.

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """Implement standard (and unused) TCP protocols.
  4. These protocols are either provided by inetd, or are not provided at all.
  5. """
  6. from __future__ import absolute_import, division
  7. import time
  8. import struct
  9. from zope.interface import implementer
  10. from twisted.internet import protocol, interfaces
  11. class Echo(protocol.Protocol):
  12. """
  13. As soon as any data is received, write it back (RFC 862).
  14. """
  15. def dataReceived(self, data):
  16. self.transport.write(data)
  17. class Discard(protocol.Protocol):
  18. """
  19. Discard any received data (RFC 863).
  20. """
  21. def dataReceived(self, data):
  22. # I'm ignoring you, nyah-nyah
  23. pass
  24. @implementer(interfaces.IProducer)
  25. class Chargen(protocol.Protocol):
  26. """
  27. Generate repeating noise (RFC 864).
  28. """
  29. noise = b'@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ !"#$%&?'
  30. def connectionMade(self):
  31. self.transport.registerProducer(self, 0)
  32. def resumeProducing(self):
  33. self.transport.write(self.noise)
  34. def pauseProducing(self):
  35. pass
  36. def stopProducing(self):
  37. pass
  38. class QOTD(protocol.Protocol):
  39. """
  40. Return a quote of the day (RFC 865).
  41. """
  42. def connectionMade(self):
  43. self.transport.write(self.getQuote())
  44. self.transport.loseConnection()
  45. def getQuote(self):
  46. """
  47. Return a quote. May be overrriden in subclasses.
  48. """
  49. return b"An apple a day keeps the doctor away.\r\n"
  50. class Who(protocol.Protocol):
  51. """
  52. Return list of active users (RFC 866)
  53. """
  54. def connectionMade(self):
  55. self.transport.write(self.getUsers())
  56. self.transport.loseConnection()
  57. def getUsers(self):
  58. """
  59. Return active users. Override in subclasses.
  60. """
  61. return b"root\r\n"
  62. class Daytime(protocol.Protocol):
  63. """
  64. Send back the daytime in ASCII form (RFC 867).
  65. """
  66. def connectionMade(self):
  67. self.transport.write(time.asctime(time.gmtime(time.time())) + b'\r\n')
  68. self.transport.loseConnection()
  69. class Time(protocol.Protocol):
  70. """
  71. Send back the time in machine readable form (RFC 868).
  72. """
  73. def connectionMade(self):
  74. # is this correct only for 32-bit machines?
  75. result = struct.pack("!i", int(time.time()))
  76. self.transport.write(result)
  77. self.transport.loseConnection()
  78. __all__ = ["Echo", "Discard", "Chargen", "QOTD", "Who", "Daytime", "Time"]