SHARE
TWEET
Untitled
a guest
Jan 3rd, 2018
52
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- 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(f"Number of connections received: %d" % (self.factory.numConnections,))
- print(f"> Received: ``%s''\n> Sending: ``%s''" % (data.decode('utf-8'), 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(10310, QuoteFactory())
- 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.
