Flask-socketio-无法设置"Access-Control-Allow-Origin"响应头 [英] Flask-socketio - failed to set "Access-Control-Allow-Origin" response header

查看:317
本文介绍了Flask-socketio-无法设置"Access-Control-Allow-Origin"响应头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这个简单的flask-socketio代码:

I wrote this simple flask-socketio code:

from flask import Flask
from flask_socketio import SocketIO, send

app = Flask(__name__)
app.config['SECRET_KEY'] = 'mysecret'

socketio = SocketIO(app)


@socketio.on('message')
def handle_message(msg):
    print 'Message:' + msg
    send(msg, broadcast=True)


if __name__ == '__main__':
    socketio.run(app)

当我看到Chrome网络分析时,可以看到"Access-Control-Allow-Origin"值为"".

When I see chrome network analyzing, I can see the "Access-Control-Allow-Origin" value as null.

根据 Flask-socketio 文档:(请参阅 API参考 @ @

According to Flask-socketio documentation: (See API Reference @ http://flask-socketio.readthedocs.io/en/latest/)

Parameters:
...
cors_allowed_origins – List of origins that are allowed to connect to this server. All origins are allowed by default.

我在搜索时发现的另一个建议是使用 flask-CORS :

Another suggestion I found on searching is using flask-CORS:

app.config['SECRET_KEY'] = 'mysecret'
cors = CORS(app)

socketio = SocketIO(app)

我得到相同的结果.

通过flask-socketio允许跨域请求的方法是什么?

What is a way to allow Cross-Origin requests with flask-socketio?

谢谢.

推荐答案

在Flask-SocketIO上默认启用交叉原点.我的猜测是,您测试此方法的方式是有缺陷的.在运行示例应用程序时,我可以向Socket.IO主端点发送请求,并在响应中得到 Access-Control-Allow-Origin 标头:

Cross origin is enabled by default on Flask-SocketIO. My guess is that the way you are testing this is flawed. While running your example application, I can send a request to the main Socket.IO endpoint and I do get the Access-Control-Allow-Origin header in the response:

~ $ http http://localhost:5000/socket.io/
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 119
Content-Type: application/octet-stream
Date: Fri, 01 Jun 2018 17:10:01 GMT
Set-Cookie: io=dd8d67788df54510830fea64bc82b1fd



+-----------------------------------------+
| NOTE: binary data not shown in terminal |
+-----------------------------------------+

这篇关于Flask-socketio-无法设置"Access-Control-Allow-Origin"响应头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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