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