daily pastebin goal
53%
SHARE
TWEET

twisted-client-2-solution-1

a guest Apr 18th, 2016 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class PoetryProtocol(Protocol):
  2.  
  3.     poem = ''
  4.     task_num = 0
  5.  
  6.     def makeConnection(self, transport):
  7.         Protocol.makeConnection(self, transport)
  8.         from twisted.internet import reactor
  9.         self.timeout = reactor.callLater(20, self.transport.loseConnection)
  10.  
  11.     def dataReceived(self, data):
  12.         self.poem += data
  13.         msg = 'Task %d: got %d bytes of poetry from %s'
  14.         print msg % (self.task_num, len(data), self.transport.getPeer())
  15.  
  16.     def connectionLost(self, reason):
  17.         if self.timeout.called is not 1:
  18.             self.timeout.cancel()
  19.         self.poemReceived(self.poem)
  20.  
  21.     def poemReceived(self, poem):
  22.         self.factory.poem_finished(self.task_num, poem)
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