Flask的响应流正在发送垃圾邮件

时间:2019-09-22 13:24:48

标签: javascript reactjs http flask flask-restful

我正在使用flask.Response从Python Flask服务器发送SSE到React.js前端中的EventSource实例。它工作正常,但是Python Flask服务器正在生成大量...GET /stream HTTP/1.1...形式的日志消息。我想知道为什么它会向日志消息发送垃圾邮件,以及如何阻止它执行此操作?

烧瓶服务器:

#!/usr/bin/env python3


import flask
from flask_cors import CORS
import random

app = flask.Flask(__name__)
app.secret_key = 'asdf'
CORS(app)

def stream_internal():
    if random.random() < 0.1:
        print("sending data")
        yield 'data: %s\n\n' % b'test'

@app.route('/stream')
def stream():
    return flask.Response(stream_internal(), mimetype="text/event-stream")


if __name__ == '__main__':
    app.debug = True
    app.run()

React.js订阅逻辑(registerCallback在前端被调用约8次):

let callbacks = []

function registerCallback(callback) {
    var eventSource = new EventSource("http://localhost:5000/stream")
    callbacks.push(callback)
    eventSource.onmessage = e => {
        var i;
        for(i = 0; i < callbacks.length; ++i) {
            // this works just fine.
            callbacks[i]( ........ )
        }
    };
}

烧瓶日志消息:

127.0.0.1 - - [22/Sep/2019 23:14:50] "GET /stream HTTP/1.1" 200 -
127.0.0.1 - - [22/Sep/2019 23:14:50] "GET /stream HTTP/1.1" 200 -
127.0.0.1 - - [22/Sep/2019 23:14:53] "GET /stream HTTP/1.1" 200 -
127.0.0.1 - - [22/Sep/2019 23:14:53] "GET /stream HTTP/1.1" 200 -
sending data
127.0.0.1 - - [22/Sep/2019 23:14:53] "GET /stream HTTP/1.1" 200 -
127.0.0.1 - - [22/Sep/2019 23:14:53] "GET /stream HTTP/1.1" 200 -
127.0.0.1 - - [22/Sep/2019 23:14:53] "GET /stream HTTP/1.1" 200 -
sending data
127.0.0.1 - - [22/Sep/2019 23:14:53] "GET /stream HTTP/1.1" 200 -
127.0.0.1 - - [22/Sep/2019 23:14:53] "GET /stream HTTP/1.1" 200 -
127.0.0.1 - - [22/Sep/2019 23:14:53] "GET /stream HTTP/1.1" 200 -
127.0.0.1 - - [22/Sep/2019 23:14:56] "GET /stream HTTP/1.1" 200 -

0 个答案:

没有答案