-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPyQuoteS.py
More file actions
34 lines (26 loc) · 1.05 KB
/
Copy pathPyQuoteS.py
File metadata and controls
34 lines (26 loc) · 1.05 KB
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
from twisted.internet.protocol import Factory
from twisted.internet import reactor, protocol
class QuoteProtocol(protocol.Protocol):
def __init__(self, factory):
self.factory = factory
def connectionMade(self):
self.factory.numConnections += 1
def dataReceived(self, data):
print("Number of active connections: %d" % ( self.factory.numConnections,))
print("> Received: ``%s''\n> Sending: ``%s''" % ( data, self.getQuote()))
self.transport.write(self.getQuote())
self.updateQuote(data)
def connectionLost(self, reason):
self.factory.numConnections -= 1
def getQuote(self):
return self.factory.quote
def updateQuote(self, quote):
self.factory.quote = quote
class QuoteFactory(Factory):
numConnections = 0
def __init__(self, quote=None):
self.quote = quote or "An apple a day keeps the doctor away"
def buildProtocol(self, addr):
return QuoteProtocol(self)
reactor.listenTCP(8000, QuoteFactory())
reactor.run()