Advertisement
Guest User

Untitled

a guest
Jun 1st, 2013
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.73 KB | None | 0 0
  1. class PoetryProtocol(Protocol):
  2.  
  3.     poem = ''
  4.  
  5.     def __init__(self):
  6.         self.timeout = None
  7.         self.if_timedout = False
  8.  
  9.     def timedout(self):
  10.         self.if_timedout = True
  11.         self.transport.loseConnection()
  12.         print 'Timed out!'
  13.  
  14.     def success(self):
  15.         if self.timeout is None or self.if_timedout is True:
  16.             return
  17.         self.timeout.cancel()
  18.  
  19.     def dataReceived(self, data):
  20.         self.poem += data
  21.  
  22.     def connectionMade(self):
  23.         self.timeout = reactor.callLater(5, self.timedout)
  24.  
  25.     def connectionLost(self, reason):
  26.         self.poemReceived(self.poem)
  27.  
  28.     def poemReceived(self, poem):
  29.         self.factory.poem_finished(poem)
  30.         self.success()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement