【发布时间】:2014-04-07 10:14:19
【问题描述】:
我对扭曲的服务器和一般的服务器有点陌生。我需要在本地运行一个异步服务器,从 twitter 推文中检索数据(将数据写入本地文件),同时,我想在“local_ip:8880”处提供一些简单的内容,例如“Hello world” 问题是流 api 创建了一个 http 连接,我不知道如何同时执行这两个任务。我的代码如下所示:
"imports and connection to twitter api keys"
def pp(o):
return json.dumps(o, indent=3)
class listener(StreamListener):
def on_data(self,data):
#Convert to JSON the input string
data_json = json.loads(data)
#print pp(data_json['user'])
#We get the html from embedly
tweet_id = data_json['id_str']
tweet_user = data_json['user']['screen_name']
tweet_url = "https://twitter.com/"+tweet_user+"/status/"+tweet_id
html="<a class=\"embedly-card\" href=\""+tweet_url+"\">Prueba</a>\n"
print "Datos recibidos en el listener."
#Write the results in the file
f = open('myfile.html','a')
f.write(html) # python will convert \n to os.linesep
f.close()
return True
def on_error(self,status):
print status
class Root(resource.Resource,StreamListener):
isLeaf = True
def __init__(self):
print "Server iniciado"
auth = OAuthHandler(CONSUMER_KEY,CONSUMER_SECRET)
auth.set_access_token(OAUTH_TOKEN,OAUTH_TOKEN_SECRET)
twitterStream = Stream(auth, listener())
twitterStream.filter(track=["query"])
def render_GET(self, request):
print "Recibida peticion"
return '<html><body>Hello world<body></html>'
root = Root()
site = server.Site(root)
reactor.listenTCP(8880, site)
reactor.run()
当我运行它时,我卡在控制台中,它接收流数据,但是当我访问我的“local_ip:8880”时,它无法加载“Hello world”。 有可能做到这一点吗?提前感谢您的关注,对我的英语感到抱歉。
【问题讨论】:
标签: python api asynchronous twitter twisted