Advertisement
Guest User

twisted-client-2-solution-1

a guest
Apr 18th, 2016
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.71 KB | None | 0 0
  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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement