Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #### Changed class PoetryProtocol from twisted-client-4\get-poetry.py
- class PoetryProtocol(Protocol):
- poem = ''
- timeoutVal = 2
- def dataReceived(self, data):
- self.poem += data
- def connectionLost(self, reason):
- if self.timeoutFn.active():
- # ok: timeout not triggered
- self.timeoutFn.cancel()
- self.poemReceived(self.poem)
- else:
- # nok: timed out
- self.poemNotRxd(Exception('Time out'))
- def poemReceived(self, poem):
- self.factory.poem_finished(poem)
- def poemNotRxd(self, reason):
- self.factory.clientConnectionFailed(self.transport.connector, reason)
- def connectionMade(self):
- from twisted.internet import reactor
- self.timeoutFn = reactor.callLater(self.timeoutVal, self.transport.loseConnection)
- #### Changed poetry_main() from twisted-client-4\get-poetry.py
- def poetry_main():
- addresses = parse_args()
- from twisted.internet import reactor
- poems = []
- errors = []
- def got_poem(poem):
- poems.append(poem)
- def poem_failed(err, host, port):
- print >>sys.stderr, 'Poem failed on %s:%d:' % (host, port), err
- errors.append(err)
- def poem_done(_):
- if len(poems) + len(errors) == len(addresses):
- reactor.stop()
- for address in addresses:
- host, port = address
- d = get_poetry(host, port)
- d.addCallbacks(got_poem, poem_failed, errbackArgs=(host, port))
- d.addBoth(poem_done)
- reactor.run()
- for poem in poems:
- print poem
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement