def on_error(self, status_code): print("status code", status_code) return True # Don't kill the stream def on_timeout(self): return True # Don't kill the stream if __name__ == '__main__': #This handles Twitter authetification and the connection to Twitter Streaming API l = MyStreamListener() auth = OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) while True: try: stream = Stream(auth, l) #This line filter Twitter Streams to capture data by the keyword-s stream.filter(track=keyword, stall_warnings=True) except Exception: pass # Oh well, reconnect and keep trucking except KeyboardInterrupt: stream.close() sys.exit(0)