Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Twisted服务器,使用Twitter流api,同时提供静态内容_Python_Api_Asynchronous_Twitter_Twisted - Fatal编程技术网

Python Twisted服务器,使用Twitter流api,同时提供静态内容

Python Twisted服务器,使用Twitter流api,同时提供静态内容,python,api,asynchronous,twitter,twisted,Python,Api,Asynchronous,Twitter,Twisted,我对twisted服务器和一般服务器有点陌生。我需要在本地运行一个异步服务器,从twitter tweets中检索数据(将数据写入本地文件),同时,我想在“local_ip:8880”上提供一些简单的内容,如“Hello world” 问题是流api创建了一个http连接,我不知道如何同时执行这两个任务。我的代码如下所示: "imports and connection to twitter api keys" def pp(o): return json.dumps(o, indent=

我对twisted服务器和一般服务器有点陌生。我需要在本地运行一个异步服务器,从twitter tweets中检索数据(将数据写入本地文件),同时,我想在“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()
“导入并连接到twitter api密钥”
def pp(o):
返回json.dumps(o,缩进=3)
类侦听器(StreamListener):
def on_数据(自身、数据):
#将输入字符串转换为JSON
data_json=json.load(数据)
#打印pp(数据_json['user'])
#我们从Embeddely获得html
tweet\u id=data\u json['id\u str']
tweet\u user=data\u json['user']['screen\u name']
tweet_url=”https://twitter.com/“+tweet\u user+”/status/“+tweet\u id”
html=“\n”
打印“Datos recibidos en el listener”
#将结果写入文件中
f=open('myfile.html','a')
f、 写入(html)#python将\n转换为os.linesep
f、 关闭()
返回真值
def on_错误(自身、状态):
打印状态
类根(resource.resource,StreamListener):
isLeaf=True
定义初始化(自):
打印“服务器iniciado”
auth=OAuthHandler(使用者密钥,使用者密钥)
身份验证设置访问令牌(OAUTH令牌、OAUTH令牌、机密)
twitterStream=流(auth,listener())
twitterStream.filter(跟踪=[“查询”])
def render_GET(自我,请求):
打印“Recibida peticion”
返回“你好,世界”
根=根()
站点=服务器。站点(根)
反应堆listenTCP(8880,现场)
反应堆运行()
当我运行这个时,我被困在控制台中,它接收流数据,但当我访问我的“local_ip:8880”时,它无法加载“Hello world”。
有可能做到这一点吗?提前感谢您的关注,并为我的英语感到抱歉。

如果您使用阻塞API(例如,使用阻塞套接字执行网络I/O的函数),那么您将阻止Twisted应用程序同时执行操作。一次只能进行一个阻塞操作


切换到非阻塞的Twitter API—例如,

我似乎需要授权才能从新的流API端点“”获取数据,而twisted Twitter流就是这样使用的。我整个下午都在寻找另一个可以工作的api,但是没有人工作,所有的api都被阻塞了。有什么建议吗?谢谢你抽出时间。