1
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
2
# See LICENSE for details.
4
"""Implement standard (and unused) TCP protocols.
6
These protocols are either provided by inetd, or are not provided at all.
11
from zope.interface import implements
14
from twisted.internet import protocol, interfaces
17
class Echo(protocol.Protocol):
18
"""As soon as any data is received, write it back (RFC 862)"""
20
def dataReceived(self, data):
21
self.transport.write(data)
24
class Discard(protocol.Protocol):
25
"""Discard any received data (RFC 863)"""
27
def dataReceived(self, data):
28
# I'm ignoring you, nyah-nyah
32
class Chargen(protocol.Protocol):
33
"""Generate repeating noise (RFC 864)"""
34
noise = r'@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ !"#$%&?'
36
implements(interfaces.IProducer)
38
def connectionMade(self):
39
self.transport.registerProducer(self, 0)
41
def resumeProducing(self):
42
self.transport.write(self.noise)
44
def pauseProducing(self):
47
def stopProducing(self):
51
class QOTD(protocol.Protocol):
52
"""Return a quote of the day (RFC 865)"""
54
def connectionMade(self):
55
self.transport.write(self.getQuote())
56
self.transport.loseConnection()
59
"""Return a quote. May be overrriden in subclasses."""
60
return "An apple a day keeps the doctor away.\r\n"
62
class Who(protocol.Protocol):
63
"""Return list of active users (RFC 866)"""
65
def connectionMade(self):
66
self.transport.write(self.getUsers())
67
self.transport.loseConnection()
70
"""Return active users. Override in subclasses."""
74
class Daytime(protocol.Protocol):
75
"""Send back the daytime in ASCII form (RFC 867)"""
77
def connectionMade(self):
78
self.transport.write(time.asctime(time.gmtime(time.time())) + '\r\n')
79
self.transport.loseConnection()
82
class Time(protocol.Protocol):
83
"""Send back the time in machine readable form (RFC 868)"""
85
def connectionMade(self):
86
# is this correct only for 32-bit machines?
87
result = struct.pack("!i", int(time.time()))
88
self.transport.write(result)
89
self.transport.loseConnection()