def getYobotPurpleFactory(self): f = ClientFactory() f.protocol = YobotPurple f.setPurple = self.setPurple f.doRegister = self.purpleDoRegister f.dispatch = self.purple_dispatch #... f.attempts = 0 max_attempts = 5 def clientConnectionFailed(connector, reason): log_err("Attempt %d/%d failed: %s" % (f.attempts, max_attempts, reason)) if f.attempts < max_attempts: log_info("trying again in 1.5 secs") reactor.callLater(1.5, connector.connect) f.attempts += 1 f.clientConnectionFailed = clientConnectionFailed return f