daily pastebin goal
94%
SHARE
TWEET

Untitled

a guest Jan 3rd, 2018 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from twisted.internet.protocol import Factory
  2. from twisted.internet import reactor, protocol
  3.  
  4. class QuoteProtocol(protocol.Protocol):
  5.     def __init__(self, factory):
  6.         self.factory = factory
  7.  
  8.     def connectionMade(self):
  9.         self.factory.numConnections +=1
  10.  
  11.     def dataReceived(self, data):
  12.         print(f"Number of connections received: %d" % (self.factory.numConnections,))
  13.         print(f"> Received: ``%s''\n> Sending: ``%s''" % (data.decode('utf-8'), self.getQuote()))
  14.         self.transport.write(self.getQuote())
  15.         self.updateQuote(data)
  16.  
  17.     def connectionLost(self, reason):
  18.         self.factory.numConnections -= 1
  19.  
  20.     def getQuote(self):
  21.         return self.factory.quote
  22.  
  23.     def updateQuote(self, quote):
  24.         self.factory.quote = quote
  25.  
  26. class QuoteFactory(Factory):
  27.     numConnections = 0
  28.  
  29.     def __init__(self, quote=None):
  30.         self.quote = quote or "An apple a day keeps the doctor away"
  31.  
  32.     def buildProtocol(self, addr):
  33.         return QuoteProtocol(self)
  34.  
  35. reactor.listenTCP(10310, QuoteFactory())
  36. reactor.run()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top