问题描述
def read_cred(file):
in_handle = open (文件'r')
cred = {}
in in_handle中的ln:
data = ln.strip('\r\\\
')。split('=')
如果len(data)> 1:
key = data [0] .strip('').lower()
value = data [1] .strip('')
cred [key] = value
打印解析凭据文件中的错误
return cred
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ act (数据)
def on_error(self,status_code,data):
print status_code,data
stream = MyStreamer(cred ['consumer_key'],cred ['consumer_secret'],
cred ['access_token_key '],cred ['access_token_secret'])
keywords = sys.argv [2]
statuses.filter(track = keywords)
但是,我想在django框架中创建一个包含开始和停止按钮的UI 。当我点击停止按钮时,应该怎么做才能停止twython流式传输?可以给我一些简单的例子吗?
可以使用文档中所述的disconnect()函数的Twython -
def on_stop(self,status_code,data):
self.disconnect()
def on_start(keywords):
stream = MyStreamer(cred ['consumer_key'],cred ['consumer_secret'],
cred ['access_token_key'] ,cred ['access_token_secret'])
stream.statuses.filter(track = keywords)
I have the codes for twython streaming and it is working.
def read_cred(file): in_handle = open(file,'r')
cred = {}
for ln in in_handle:
data = ln.strip('\r\n').split('=')
if len(data) > 1:
key = data[0].strip(' ').lower()
value = data[1].strip(' ')
cred[key] = value
else:
print "error in parsing credentials file"
return cred
cred = read_cred(sys.argv[1])
class MyStreamer(TwythonStreamer): def on_success(self, data): act(data)
def on_error(self, status_code, data):
print status_code, data
stream = MyStreamer(cred['consumer_key'], cred['consumer_secret'], cred['access_token_key'], cred['access_token_secret'])
keywords = sys.argv[2]
stream.statuses.filter(track=keywords)
However, I want to create a UI in django framework which consist of a 'start' and a 'stop' button. What should I do to stop the twython streaming when I clicked on the button 'stop' ? Can give me some simple examples pls?
You can use the disconnect() function as described in the documentation of Twython - https://twython.readthedocs.org/en/latest/api.html#twython.TwythonStreamer.disconnect
def on_stop(self, status_code, data):
self.disconnect()
def on_start(keywords):
stream = MyStreamer(cred['consumer_key'], cred['consumer_secret'],
cred['access_token_key'], cred['access_token_secret'])
stream.statuses.filter(track=keywords)
这篇关于在某些时间点可以停止twython流式传输吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!