Flask:一个RESTful API和SocketIO服务器 [英] Flask: A RESTful API and SocketIO Server

查看:1123
本文介绍了Flask:一个RESTful API和SocketIO服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图用Flask-RESTful扩展创建一个简单的REST API。这个API将主要用于管理简单服务的用户的CRUD和认证。

我也尝试使用Flask-SocketIO扩展创建一些Web套接字,这些用户将能够连接并查看某些数据相关的实时更新给使用该服务的其他人。因此,我需要知道这些用户已经通过身份验证并被授权连接到特定的套接字。

问题



然而,我在设置时遇到了一些麻烦。看起来我不能让这两个组件(REST API和SocketIO服务器)在同一个Flask实例上一起工作。我之所以这么说,是因为当我运行以下时,REST API或者SocketIO服务器都可以工作,但不能同时工作:

  from flask进口烧瓶
from flask_restful进口Api $ b $ from flask.ext.socketio import SocketIO

app = Flask(__ name__)

api = API(应用程序)
socketio = SocketIO(应用程序)

API的一些测试资源和
#添加了一个用于SocketIO服务器
#的测试发射器语句这里
$ b $如果__name__ =='__main__':
app.run(port = 5000)
socketio.run(app,port = 5005)


问题

是典型的解决方案这种类型的设置有两个不同的Flask实例在同一时间吗?例如,我的SocketIO服务器是否必须向我的REST API发出请求,以检查某个特定的用户是否被授权连接到一个特定的套接字?

socketio.run(app,port = 5005),然后在端口5005上点击REST API。



这个原因起作用的原因在于,Flask-SocketIO正在运行一个基于gevent(或者1.0版本,也是eventlet)的偶数web服务器 - 这个服务器处理websocket直接请求(使用通过 socketio.on 装饰器注册的处理程序),并将非websocket请求传递给Flask。



你的代码不工作的原因是因为 app.run socketio.run 阻止操作。无论哪个人首先跑的都是循环,等待连线,从不让第二个跑开。如果你真的需要在不同的端口上运行你的websocket连接,你需要产生一个不同的socketio或者应用程序 run 过程。

Background

I am trying to create a simple REST API using the Flask-RESTful extension. This API will be working primarily to manage the CRUD and authentication of users for a simple service.

I am also trying to create a few web sockets using the Flask-SocketIO extension that these users will be able to connect to and see real-time updates for some data related to other people using the service. As such, I need to know that these users are authenticated and authorized to connect to certain sockets.

Problem

However, I'm having a bit of trouble getting set up. It seems like I am not able to have these two components (the REST API and SocketIO server) work together on the same Flask instance. The reason I say this is because when I run the following, either the REST API or the SocketIO server will work, but not both:

from flask import Flask
from flask_restful import Api
from flask.ext.socketio import SocketIO

app = Flask(__name__)

api = Api(app)
socketio = SocketIO(app)

# some test resources for the API and
# a test emitter statement for the SocketIO server
# are added here

if __name__ == '__main__':
    app.run(port=5000)
    socketio.run(app, port=5005)

Question

Is the typical solution for this type of setup to have two distinct instances of Flask going at the same time? For instance, would my SocketIO server have to make requests to my REST API in order to check to see that a specific user is authenticated/authorized to connect to a specific socket?

解决方案

You just want to run socketio.run(app, port=5005) and hit the REST API on port 5005.

The reason this works is because under the hood, Flask-SocketIO is running an evented webserver based on gevent (or with the 1.0 release, also eventlet) - this server handling the websocket requests directly (using the handlers you register via the socketio.on decorator) and is passing on the non-websocket requests to Flask.

The reason your code wasn't working is because both app.run and socketio.run are blocking operations. Whichever one ran first was looping, waiting for connections, never allowing the second to kick off. If you really needed to run your websocket connections on a different port you'd need to spawn either the socketio or the app run call on a different process.

这篇关于Flask:一个RESTful API和SocketIO服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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