简体   繁体   English

使用eventlet来管理Flask中的socketio

[英]Using eventlet to manage socketio in Flask

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. 我正在尝试设置一个小型服务器来处理HTTP和socketio请求 - 我没有太多设置服务器的经验,但现在apache2服务http就好了。 The socketio transactions, however, keep failing with error code 400 (bad request), and I see some strange errors in the server logs. 然而,socketio事务继续失败,错误代码为400(错误请求),我在服务器日志中看到一些奇怪的错误。 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: 有时我看到一个engineio错误,服务器响应“错误请求”和代码400,但总是它告诉我需要启动eventlet服务器:

[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)

And my client code, written in python, uses the socketio-client library straight from the docs: 我的客户端代码,用python编写,直接使用文档中的socketio-client库:

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

Isn't the socketio.run(app) supposed to start the eventlet server for me? 是不是socketio.run(app)应该为我启动eventlet服务器? Why is the server spitting back bad request (sometimes)? 为什么服务器吐出错误的请求(有时)?

To make a WSGI application available online you need to expose it through a web server. 要使WSGI应用程序在线可用,您需要通过Web服务器公开它。 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时,一个普通的WSGI Web服务器是不够的,因为WSGI不支持WebSocket,WSGI协议需要非官方扩展来支持该协议。

Flask-SocketIO supports a variety of web servers that support WebSocket. Flask-SocketIO支持各种支持WebSocket的Web服务器。 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. 您似乎在虚拟环境中安装了eventlet,因此这就是您收到必须使用eventlet Web服务器的错误的原因。

What you don't seem to realize, is that you are using Apache's web server (I'm guessing mod_wsgi?). 您似乎没有意识到的是,您正在使用Apache的Web服务器(我在猜mod_wsgi?)。 This web server is a normal, forking web server, it is not an eventlet compatible web server. 此Web服务器是正常的分叉Web服务器,它不是与Eventlet兼容的Web服务器。

Isn't the socketio.run(app) supposed to start the eventlet server for me? 是不是socketio.run(app)应该为我启动eventlet服务器?

Yes, if you were to run your application via socketio.run(app) you would get a fully enabled eventlet web server. 是的,如果您通过socketio.run(app)运行您的应用socketio.run(app)您将获得一个完全启用的eventlet Web服务器。 But you are not doing that, you are running it on apache. 但是你没有这样做,你是在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. Eventlet有一个Web服务器,apache有一个Web服务器,它们是两个独立的Web服务器,都能够运行WSGI应用程序。 But the apache one does not support WebSocket. 但apache一个不支持WebSocket。

The Flask-SocketIO documentation describes a few deployment scenarios that are valid. Flask-SocketIO文档描述了一些有效的部署方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM