Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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(b'self.factory.quote')
- def dataReceived(self, data):
- print(f"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("connecton failed:"), reason.getErrorMessage()
- maybeStopReactor()
- def clientConnectionLost(self, connector, reason):
- print("connection lost"), 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', 10310, QuoteClientFactory(quote))
- reactor.run()
Advertisement
Add Comment
Please, Sign In to add comment