Flask和Flask SocketIO:从外部模块内部接收事件

时间:2017-03-03 07:13:43

标签: python flask flask-socketio

我正在开发一个应用程序,用于从科学仪器中读取一些信息并使用远程Web客户端显示它。我用Flask和Flask-SocketIO实现了这一目标。我有一个主application.py文件,其中包含一个名为app的Flask实例。我和Flask-SocketIO一起使用它。 Flask部署的HTML页面包含一个Javascript Socketio实例,它与服务器应用程序通信(在python中)。

此外,我有一个代表我的科学仪器的外部模块。我想从网站(在Javascript中)接收到这个模块类的事件。我尝试将appsocketio python对象传递给构造函数,以便从Flask app请求上下文。这非常有效仅用于从模块发出事件。

简而言之,application.py具有以下基本结构:

from gevent import monkey
monkey.patch_all()
from flask import Flask, Response, render_template, session, request, g
from flask_socketio import SocketIO, emit, disconnect, Namespace
from Instrument import *

app = Flask(__name__)
app.debug = True
socketio = SocketIO(app)

# some stuff here with @app.routes
@app.route('/instrument')
def inst(arg):
    t = Instrument(arg, app, socketio)

if __name__ == '__main__':
    socketio.run(app, port=8000, log_output=True)

对于外部模块Instrument.py我尝试使用装饰器语法失败(@self.socketio.on种类,我对它们并不熟悉)。所以我尝试使用以下代码:

class Instrument():
    def __init__(self, arg, app, socketio):
        self.app = app
        self.socketio = socketio
        self.socketio.on_event('someCoolEvent', self.someCoolMethod, namespace='/coolapp') # THIS GIVES ME ERRORS

    def someCoolMethod():
        with self.app.test_request_context('/coolapp'):
            self.socketio.emit('emittedEvent', namespace='/telescope') # THIS WORKS FINE

我得到的错误如下(EDITED:添加了两行可能有用):

[2017-03-03 06:31:31,456][INFO] - _handle_event: received event "my event" from a40724e9e60e4a61ace9e19e59ceabda [/telescope]
[2017-03-03 06:31:31,604][INFO] - handle_get_request: a40724e9e60e4a61ace9e19e59ceabda: Received request to upgrade to websocket
[2017-03-03 06:31:33 +0000] [6333] [ERROR] Error handling request /socket.io/?EIO=3&transport=websocket&sid=24e7d134ea4c4840888c28e0e3ff1f6d
Traceback (most recent call last):
  File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/gunicorn/workers/async.py", line 52, in handle
    self.handle_request(listener_name, req, client, addr)
  File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/gunicorn/workers/ggevent.py", line 152, in handle_request
    super(GeventWorker, self).handle_request(*args)
  File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/gunicorn/workers/async.py", line 103, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
  File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/flask/app.py", line 1994, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/flask_socketio/__init__.py", line 42, in __call__
    start_response)
  File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/engineio/middleware.py", line 47, in __call__
    return self.engineio_app.handle_request(environ, start_response)
  File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/socketio/server.py", line 353, in handle_request
    return self.eio.handle_request(environ, start_response)
  File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/engineio/server.py", line 260, in handle_request
    environ, start_response)
  File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/engineio/socket.py", line 86, in handle_get_request
    start_response)
  File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/engineio/socket.py", line 127, in _upgrade_websocket
    return ws(environ, start_response)
  File "/home/user/instr_gui/venv/local/lib/python2.7/site-packages/engineio/async_gevent.py", line 34, in __call__
    raise RuntimeError('You need to use the gevent-websocket server. '
RuntimeError: You need to use the gevent-websocket server. See the Deployment section of the documentation for more information.

初始化应用时,日志显示:

2017-03-04 02:52:04 [23530] [INFO] Starting gunicorn 18.0
2017-03-04 02:52:04 [23530] [INFO] Listening at: http://127.0.0.1:8000 (23530)
2017-03-04 02:52:04 [23530] [INFO] Using worker: gevent
2017-03-04 02:52:04 [23534] [INFO] Booting worker with pid: 23534

所以我尝试使用/etc/init/myapp.confgunicorn -k gevent -w 1 module:app配置文件gunicorn -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker -w 1 module:app,但我收到同样的错误。

在浏览器中,我看到另一个错误,它与“主要”错误同时出现:

WebSocket connection to 'ws://somewebpage.com:2080/socket.io/?
EIO=3&transport=websocket&sid=572923d1b8fd402795bba50823941520' 
failed: Error during WebSocket handshake: Unexpected response code: 500

如何从外部模块正确接收事件?也许有更好的方法来实现我想要的结果。如果有人帮我一把,我将非常感激。

3 个答案:

答案 0 :(得分:1)

我自己回答。我找到了following post,它解释了错误:

WebSocket connection to 'ws://somewebpage.com:2080/socket.io/?
EIO=3&transport=websocket&sid=572923d1b8fd402795bba50823941520' 
failed: Error during WebSocket handshake: Unexpected response code: 500

是由于nginx配置文件中的配置错误引起的。它应该是这样的:

...
location / {
    proxy_pass http://127.0.0.1:8000;
    proxy_redirect off;

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";

    proxy_set_header Host $http_host;
}
...

现在就像魅力:从导入的模块和主python脚本发出和接收事件。

答案 1 :(得分:0)

您可以尝试在policy_server=False

中添加socketio.run(app, port=8000, log_output=True,policy_server=False)吗?

答案 2 :(得分:0)

我认为你得到的错误与你的问题无关。它说你正在使用标准的gunicorn worker gevent,但是你需要使用gevent-websocket worker来使websocket工作。请参阅本文档section中的最后一个示例。