我在下面列出了我正在使用Tweepy的代码,这是一个用于Python的Twitter API库。虽然我正在尝试我在网上找到的大多数方法,但他们无法关闭连接或停止流。有没有办法这样做?
在我的职能范围内
setTerms = s.split(',')
streaming_api = tweepy.Stream(auth=auth, listener=StreamListener(), timeout=60 )
if (s == '0'):
streaming_api.disconnect()
raise web.seeother('/dc')
print "Failed to see this"
try:
twt = streaming_api.filter(track=setTerms)
except:
streaming_api.disconnect()
#also cannot see this
raise web.seeother('/stream')
这是流监听器类
class StreamListener(tweepy.StreamListener):
def on_status(self, status):
try:
printer(status.text, status.created_at)
except Exception, e:
pass
def on_error(self, status_code):
print >> sys.stderr, 'Encountered error with status code:', status_code
return True
def on_timeout(self):
print >> sys.stderr, 'Timeout...'
return True
答案 0 :(得分:0)
您第一次拨打stream.disconnect()
(在if (s == '0'):
内)时,尚未调用filter
,因此该流将永远不会被连接。假设您使用的是最新版本的tweepy
,其余代码应该是正确的。请注意,几乎永远不会调用except
块,因为在流运行时发生的任何错误都会传递给on_error
回调。