Flask框架可以从服务器向客户端浏览器发送实时数据吗? [英] Can flask framework send real-time data from server to client browser?

查看:327
本文介绍了Flask框架可以从服务器向客户端浏览器发送实时数据吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道flask如何(如果有的话)执行长时间轮询,以便服务器可以通过连接将数据发送到客户端。例如,如果服务器通过流式API接收到Twitter提要,该提要将如何传递到客户端浏览器?

I was wondering how (if at all) flask performs long polling, so the server can send data over a connection to the client. For example if the server receives a twitter feed via the streaming api how will that be passed to the client browser?

我收集到您不能使用flask.flash进行此类操作

I gather that you cannot use flask.flash for such a situation.

谢谢

感谢示例。我看了看这些示例,当我尝试为我的代码实现它时,它仍然没有在客户端浏览器中提供实时输出。

Thanks for the examples. I looked at the examples and when I try to implement it for my code, it still does not provide a real-time output in the client browser.

我已经使用剑圣和redis将其围绕在烧瓶snippet()周围。这是我的python代码:

I have based it around the flask snippet() using juggernaut and redis. This is my python code:

import flask
from flask.views import MethodView
from tweetStreamsRT import StreamerRt 
from juggernaut import Juggernaut


app = flask.Flask(__name__)
app.secret_key = "xxxxx"
PORT = 8080

class View(MethodView):

    def get(self):
        return flask.render_template('index.html')

    def post(self):
        results = StreamerRt().filter(track=[flask.request.form['event']])            
        jug = Juggernaut()
        jug.publish('channel', results)
        return self.get()


app.add_url_rule('/', view_func = View.as_view('index'), methods=['GET', 'POST'])
app.debug = True

if __name__ == "__main__":
    print 'Listening on http://localhost:%s' % PORT
    app.run()

我的html页面是从基本html页面继承的:

My html page is, which inherits from a base html page:

{% extends "base.html" %}
{% import "forms.html" as forms %}


{% block page_header %}
  <div class="page-header">
    <h1>Welcome</h1>
  </div>
{% endblock %}
{% block content %}
  <h2>Enter the Event you would like to follow</h2>
      <form action="/" method="post">
            <input type="text" name="event" />
            <input type="submit" value="Submit Query" />
          </form>
            Results:
            <pre>
                <script type="text/javascript" charset="utf-8">
                    var jug = new Juggernaut;
                    jug.subscribe("channel", function(data){
                    alert("Got data: " + data);});
                </script>

            </pre> 
{% endblock %}

我仍然很困惑为什么什么都没发送到客户端浏览器。

I'm still confused as to why nothing is sent to the client browser.

谢谢

推荐答案

gevent + socketio

  • an example app using Flask with gevent+socketio.
  • a socket.io route in Flask
  • gevent-socketio

这篇关于Flask框架可以从服务器向客户端浏览器发送实时数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆