在Flask中使用Eventlet管理socketio [英] Using eventlet to manage socketio in Flask

查看:259
本文介绍了在Flask中使用Eventlet管理socketio的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个小型服务器来处理HTTP和socketio请求-我在设置服务器方面没有太多经验,但是apache2现在可以很好地服务于http.但是,socketio事务始终失败,并显示错误代码400(错误的请求),并且我在服务器日志中看到一些奇怪的错误.有时,我会看到一个引擎错误,服务器会响应一个错误请求"并返回代码400,但始终会告诉我该事件服务器需要启动:

I am trying to set up a small server to handle HTTP and socketio requests -- I don't have much experience setting up servers, but right now apache2 serves the http just fine. The socketio transactions, however, keep failing with error code 400 (bad request), and I see some strange errors in the server logs. Sometimes I see an engineio error and the server responds w/ a 'bad request' and code 400, but always it tells me the eventlet server needs to be started:

[Mon Jan 11 19:02:54.068282 2016] [:error] [pid 4908:tid 140274923673344] [client 100.96.180.39:53473]     return ws(environ, start_response)
[Mon Jan 11 19:02:54.068305 2016] [:error] [pid 4908:tid 140274923673344] [client 100.96.180.39:53473]   File "/var/www/projectENV/lib/python2.7/site-packages/engineio/async_eventlet.py", line 10, in __call__
[Mon Jan 11 19:02:54.068342 2016] [:error] [pid 4908:tid 140274923673344] [client 100.96.180.39:53473]     raise RuntimeError('You need to use the eventlet server.')
[Mon Jan 11 19:02:54.068380 2016] [:error] [pid 4908:tid 140274923673344] [client 100.96.180.39:53473] RuntimeError: You need to use the eventlet server. See the Deployment section of the documentation for more information.
[Mon Jan 11 19:02:54.253124 2016] [:error] [pid 4909:tid 140274940458752] WARNING:engineio:Invalid session cde3f9aadbee4794bf9d7bb98d0b396e

我的服务器代码非常基本:

My server code is pretty basic:

 from flask import Flask
 import flaskext.couchdb
 from flask.ext.socketio import SocketIO

 # for socketio
 import eventlet
 eventlet.monkey_patch()

 # creation of server & db objects
 app = Flask(__name__)

 # socketio initialization
 socketio =  SocketIO(app, async_mode='eventlet')

 # import views once site properties are set
 from app import views

 if __name__== "__main__":
     socketio.run(app, debug=True)

我的用python编写的客户端代码直接使用了文档中的socketio-client库:

And my client code, written in python, uses the socketio-client library straight from the docs:

from socketIO_client import SocketIO, LoggingNamespace
with SocketIO(SERVER_URL, 80, LoggingNamespace) as socketIO:
    socketIO.emit('aaa')
    socketIO.wait(seconds=1)

socketio.run(app)是否应该为我启动eventlet服务器?为什么服务器有时会吐出错误的请求?

Isn't the socketio.run(app) supposed to start the eventlet server for me? Why is the server spitting back bad request (sometimes)?

推荐答案

要使WSGI应用程序在线可用,您需要通过Web服务器公开它.当您的应用程序使用Flask-SocketIO时,仅使用普通的WSGI Web服务器是不够的,因为WSGI不支持WebSocket,所以WSGI协议需要非官方的扩展才能支持该协议.

To make a WSGI application available online you need to expose it through a web server. When your application uses Flask-SocketIO, a plain WSGI web server isn't sufficient, because WSGI does not support WebSocket, the WSGI protocol needs unofficial extensions to support this protocol.

Flask-SocketIO支持各种支持WebSocket的Web服务器.看来您已在虚拟环境中安装了eventlet,因此这就是为什么您收到必须使用eventlet Web服务器的错误的原因.

Flask-SocketIO supports a variety of web servers that support WebSocket. It appears you have eventlet installed in your virtual environment, so that is why you receive the error that you have to use the eventlet web server.

您似乎没有意识到,您正在使用Apache的Web服务器(我猜是mod_wsgi吗?).该Web服务器是普通的分叉Web服务器,不是兼容Eventlet的Web服务器.

What you don't seem to realize, is that you are using Apache's web server (I'm guessing mod_wsgi?). This web server is a normal, forking web server, it is not an eventlet compatible web server.

socketio.run(app)是否应该为我启动eventlet服务器?

Isn't the socketio.run(app) supposed to start the eventlet server for me?

是的,如果要通过 socketio.run(app)运行应用程序,则将获得一个完全启用的eventlet Web服务器.但是您没有这样做,而是在apache上运行它.Eventlet具有Web服务器,而apache具有Web服务器,它们是两个独立的Web服务器,都能够运行WSGI应用程序.但是apache不支持WebSocket.

Yes, if you were to run your application via socketio.run(app) you would get a fully enabled eventlet web server. But you are not doing that, you are running it on apache. Eventlet has a web server, and apache has a web server, they are two separate web servers, both able to run a WSGI application. But the apache one does not support WebSocket.

Flask-SocketIO文档描述了一些有效的部署方案

The Flask-SocketIO documentation describes a few deployment scenarios that are valid.

这篇关于在Flask中使用Eventlet管理socketio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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