首頁  >  問答  >  主體

python - flask_socketio使用uswgi启动访问socketio会400错误

python依赖:

  1. flask

  2. flask_socketio

  3. uwsgi

  4. gevent

/web/__init__.py

from flask import Flask
from flask_socketio import SocketIO, emit
app = Flask(__name__)
socket_io = SocketIO(app)

@socket_io.on('test')
def handle_my_custom_event(json):
    print(json)
    emit('my response', json)

/web/static/index.html

<script type="text/javascript" src="./socket.io.min.js"></script>
<script type="text/javascript" charset="utf-8">
    var socket = io.connect('http://' + document.domain + ':' + location.port);
    socket.on('connect', function() {
        socket.emit('test', {data: 'I\'m connected!'});
    });
</script>

/run.sh

#!/bin/sh
uwsgi --http :8000 --gevent 1000 --http-websockets --master --module web --callable app

然后访问http://127.0.0.1:8000/static/index.html。抓包400错误无法访问socket.io

上的使用方法来自flask_socketio文档。其中下面的使用nginx代理的方式也用过了。没有用一样是400。

上面的uwsgi只能在linux上用。。我全部都是在docker中做的测试。

PHP中文网PHP中文网2741 天前899

全部回覆(1)我來回復

  • 黄舟

    黄舟2017-04-18 09:57:46

    uwsgi 不是了解。
    我用的是Gunicorn,也遇到相同問題。
    查看文件後使用自訂gevent web server解決了。

    When using gunicorn with the gevent worker and the WebSocket support provided by gevent-websocket, the command that starts the server must be changed to select a custom
    gunicorn -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker -w 1 module:app

    回覆
    0
  • 取消回覆