-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPyQuoteC.py
More file actions
39 lines (36 loc) · 1.21 KB
/
Copy pathPyQuoteC.py
File metadata and controls
39 lines (36 loc) · 1.21 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
35
36
37
38
39
from twisted.internet import reactor, protocol
class QuoteProtocol(protocol.Protocol):
def __init__(self, factory):
self.factory = factory
def connectionMade(self):
self.sendQuote()
def sendQuote(self):
self.transport.write(self.factory.quote)
def dataReceived(self, data):
print "Received quote:", data
self.transport.loseConnection()
class QuoteClientFactory(protocol.ClientFactory):
def __init__(self, quote):
self.quote = quote
def buildProtocol(self, addr):
return QuoteProtocol(self)
def clientConnectionFailed(self, connector, reason):
print('connection failed: %s' % reason.getErrorMessage())
maybeStopReactor()
def clientConnectionLost(self, connector, reason):
print('connection lost:%s'% reason.getErrorMessage())
maybeStopReactor()
def maybeStopReactor():
global quote_counter
quote_counter -= 1
if not quote_counter:
reactor.stop()
quotes = [
"You snooze you lose",
"The early bird gets the worm",
"Carpe diem"
]
quote_counter = len(quotes)
for quote in quotes:
reactor.connectTCP('localhost', 8000, QuoteClientFactory(quote))
reactor.run()