def run(self): def except_on_closed(*args): raise ClosedException("Twitter closed the stream!") while True: logging.info("Looping to start the tweeter; watching for %s and replying with %s", self.watchword, self.reply) try: tStream = Stream(auth, WatchwordListener(self.watchword, self.reply)) tStream.on_closed = except_on_closed tStream.userstream() except Exception as e: logging.exception("Encountered an error: %s", e) time.sleep(20)