Skip to main content
 首页 » 编程设计

python之flask 和 flask SocketIO : receiving an event from inside of an external module

2025年02月15日8虾米哥

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

此外,我还有一个代表我的科学仪器的外部模块。我想从网站(使用 Javascript)接收事件到这个模块类。我尝试将 app 和 socketio 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 

我收到的错误如下(已编辑:添加了两行有用的内容):

[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 

所以我尝试使用 gunicorn -k gevent -w 1 module:app 和 gunicorn - 配置文件 /etc/init/myapp.conf 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 

如何正确接收来自外部模块的事件?也许有更好的方法可以达到我想要的结果。如果有人帮我解决这个问题,我将不胜感激。

请您参考如下方法:

我自己回答。我找到了 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 脚本发出和接收事件。