最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【未解决】Flask-SocketIO测试出错:POST socket.io EIO 400 BAD REQUEST

Flask crifan 8186浏览 0评论

折腾:

【已解决】Flask-SocketIO测试出错:WebSocket connection to failed Error during WebSocket handshake Unexpected response code 400

期间,又出现GET或POST时出错:

socket.io.min.js:1 POST http://115.29.173.126:21085/socket.io/?EIO=3&transport=polling&t=1477646293352-1&sid=3387ae2af69446b696b60a506c2259d2 400 (BAD REQUEST)
socket.io.min.js:1 GET http://115.29.173.126:21085/socket.io/?EIO=3&transport=polling&t=1477646293356-2&sid=3387ae2af69446b696b60a506c2259d2 400 (BAD REQUEST)

如图:

[2016-10-28 17:19:01 +0000] [30618] [CRITICAL] WORKER TIMEOUT (pid:30670)
[2016-10-28 17:19:01 +0000] [30670] [INFO] Worker exiting (pid: 30670)

搜:

Flask-SocketIO GET socket.io 400 BAD REQUEST

Flask-SocketIO net ERR_EMPTY_RESPONSE

参考:

Welcome to Flask-SocketIO’s documentation! — Flask-SocketIO documentation

把:

    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.5/socket.io.min.js"></script>

换成:

    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.6/socket.io.min.js"></script>

试试,结果问题依旧。

去把之前的FLASK_SECRET_KEY改为SECRET_KEY:

/Users/crifan/dev/dev_root/daryun/Projects/RunningFast/sourcecode/RunningFast-Server/instance/config.py

# FLASK_SECRET_KEY = ‘\t\x9cw\x85&8\x99\x92\x81\xaa\xea\x9e\r\xd6H\xa26.\xf0\xa4\xb2\x92\xf0\xa6’
SECRET_KEY = ‘\t\x9cw\x85&8\x99\x92\x81\xaa\xea\x9e\r\xd6H\xa26.\xf0\xa4\xb2\x92\xf0\xa6’

/Users/crifan/dev/dev_root/daryun/Projects/RunningFast/sourcecode/RunningFast-Server/runningfast/app.py

# app.secret_key = app.config[‘FLASK_SECRET_KEY’]
app.secret_key = app.config[‘SECRET_KEY’]

问题依旧

-》本身应该的确也没有什么影响。

Flask-SocketIO socket.io.min.js  GET 400 (BAD REQUEST

javascript – SocketIO 400 Bad Request Error – Stack Overflow

javascript – flask socketio 400 (BAD REQUEST) – Stack Overflow

Client keeps connecting/reconnecting (400 Bad Request errors) · Issue #315 · miguelgrinberg/Flask-SocketIO · GitHub

此处有点点进步:

然后通过给SocketIO加上调试参数engineio_logger=True:

# socketio = SocketIO(app)
socketio = SocketIO(app, engineio_logger=True)

之后,可以看到输出的io的log了:

dd64d6f2dc9d4bd18109ea7263905f0b: Sending packet OPEN data {‘pingInterval’: 25000, ‘pingTimeout’: 60000, ‘upgrades’: [], ‘sid’: ‘dd64d6f2dc9d4bd18109ea7263905f0b’}
dd64d6f2dc9d4bd18109ea7263905f0b: Sending packet MESSAGE data 0
dd64d6f2dc9d4bd18109ea7263905f0b: Received packet MESSAGE data 0/test
Connected socket IO
Invalid session dd64d6f2dc9d4bd18109ea7263905f0b

<div–<——————————————————————————

DEBUG in app [/root/RunningFast/staging/runningfast/app.py:189]:
Connected socket IO

<div–<——————————————————————————

Invalid session c630cb224afe454cac5c53b1a610e7b1
c630cb224afe454cac5c53b1a610e7b1: Sending packet MESSAGE data 2/test,["my response",{"data":"Connected socket IO"}]
c630cb224afe454cac5c53b1a610e7b1: Sending packet MESSAGE data 0/test
c630cb224afe454cac5c53b1a610e7b1: Received packet CLOSE data None
5e12a59d21a84efe9b08e248d9c3a072: Sending packet OPEN data {‘pingInterval’: 25000, ‘pingTimeout’: 60000, ‘upgrades’: [], ‘sid’: ‘5e12a59d21a84efe9b08e248d9c3a072’}
5e12a59d21a84efe9b08e248d9c3a072: Sending packet MESSAGE data 0
Invalid session 5e12a59d21a84efe9b08e248d9c3a072
Invalid session 5e12a59d21a84efe9b08e248d9c3a072
209130a594c548a38f9067c9853679f4: Sending packet OPEN data {‘pingInterval’: 25000, ‘pingTimeout’: 60000, ‘upgrades’: [], ‘sid’: ‘209130a594c548a38f9067c9853679f4’}
209130a594c548a38f9067c9853679f4: Sending packet MESSAGE data 0
209130a594c548a38f9067c9853679f4: Received packet MESSAGE data 0/test

<div–<——————————————————————————

DEBUG in app [/root/RunningFast/staging/runningfast/app.py:189]:
Connected socket IO

<div–<——————————————————————————

209130a594c548a38f9067c9853679f4: Sending packet MESSAGE data 2/test,["my response",{"data":"Connected socket IO"}]
209130a594c548a38f9067c9853679f4: Sending packet MESSAGE data 0/test

搜:

Sending packet OPEN data pingInterval  pingTimeout 60000 upgrades sid

值得参考的代码:

Flask-SocketIO/app.py at master · miguelgrinberg/Flask-SocketIO · GitHub

Session disconnected issue · Issue #248 · miguelgrinberg/Flask-SocketIO · GitHub

[2016-10-28 20:00:16 +0000] [1345] [CRITICAL] WORKER TIMEOUT (pid:1355)
[2016-10-28 20:00:16 +0000] [1355] [INFO] Worker exiting (pid: 1355)
[2016-10-28 20:00:16 +0000] [1428] [INFO] Booting worker with pid: 1428
Server initialized for gevent.

<div–<——————————————————————————

DEBUG in app [/root/RunningFast/staging/runningfast/app.py:82]:
app=<Flask ‘runningfast.app’>, api=<flask_restful.Api object at 0x7f82c2c2d350>, redis_store=<flask_redis.FlaskRedis object at 0x7f82c13cbad0>, db=<SQLAlchemy engine=’mysql://runningfast:Jiandao123@localhost/runningfast_dev’>, server_mode=staging, server_type=develop, rq=<flask_rq2.app.RQ object at 0x7f82c2c2d790>, socketio=<flask_socketio.SocketIO object at 0x7f82c2c2db10>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in app [/root/RunningFast/staging/runningfast/app.py:219]:
API_VERSION=1.0, API_URL_PREFIX=/runningfast/api/v1.0, OPEN_API_URL_PREFIX=/runningfast/api/v1.0/open

<div–<——————————————————————————

[2016-10-28 20:00:18 +0000] [1345] [CRITICAL] WORKER TIMEOUT (pid:1356)
[2016-10-28 20:00:18 +0000] [1356] [INFO] Worker exiting (pid: 1356)
[2016-10-28 20:00:18 +0000] [1432] [INFO] Booting worker with pid: 1432
Server initialized for gevent.

<div–<——————————————————————————

DEBUG in app [/root/RunningFast/staging/runningfast/app.py:82]:
app=<Flask ‘runningfast.app’>, api=<flask_restful.Api object at 0x7f82c2c2d350>, redis_store=<flask_redis.FlaskRedis object at 0x7f82c13cbad0>, db=<SQLAlchemy engine=’mysql://runningfast:Jiandao123@localhost/runningfast_dev’>, server_mode=staging, server_type=develop, rq=<flask_rq2.app.RQ object at 0x7f82c2c2d790>, socketio=<flask_socketio.SocketIO object at 0x7f82c2c2db10>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in app [/root/RunningFast/staging/runningfast/app.py:219]:
API_VERSION=1.0, API_URL_PREFIX=/runningfast/api/v1.0, OPEN_API_URL_PREFIX=/runningfast/api/v1.0/open

<div–<——————————————————————————

bde917d435ad42b7ac1a1d3a6a68e727: Sending packet OPEN data {‘pingInterval’: 25000, ‘pingTimeout’: 60000, ‘upgrades’: [], ‘sid’: ‘bde917d435ad42b7ac1a1d3a6a68e727’}
bde917d435ad42b7ac1a1d3a6a68e727: Sending packet MESSAGE data 0
Invalid session bde917d435ad42b7ac1a1d3a6a68e727
Invalid session bde917d435ad42b7ac1a1d3a6a68e727
bde917d435ad42b7ac1a1d3a6a68e727: Received packet CLOSE data None
[2016-10-28 20:00:31 +0000] [1345] [CRITICAL] WORKER TIMEOUT (pid:1363)
[2016-10-28 20:00:31 +0000] [1363] [INFO] Worker exiting (pid: 1363)
[2016-10-28 20:00:31 +0000] [1443] [INFO] Booting worker with pid: 1443
Server initialized for gevent.

<div–<——————————————————————————

DEBUG in app [/root/RunningFast/staging/runningfast/app.py:82]:
app=<Flask ‘runningfast.app’>, xxx, socketio=<flask_socketio.SocketIO object at 0x7f82c2c2db10>

<div–<——————————————————————————

00d06db801b04aad84d2c2264f632af8: Sending packet OPEN data {‘pingInterval’: 25000, ‘pingTimeout’: 60000, ‘upgrades’: [], ‘sid’: ’00d06db801b04aad84d2c2264f632af8′}
00d06db801b04aad84d2c2264f632af8: Sending packet MESSAGE data 0
Invalid session 00d06db801b04aad84d2c2264f632af8
Invalid session 00d06db801b04aad84d2c2264f632af8

<div–<——————————————————————————

DEBUG in app [/root/RunningFast/staging/runningfast/app.py:219]:
API_VERSION=1.0, API_URL_PREFIX=/runningfast/api/v1.0, OPEN_API_URL_PREFIX=/runningfast/api/v1.0/open

<div–<——————————————————————————

00d06db801b04aad84d2c2264f632af8: Received packet CLOSE data None
a2c4e03b4a2b4f7f8936e5a9d0f8b6a3: Sending packet OPEN data {‘pingInterval’: 25000, ‘pingTimeout’: 60000, ‘upgrades’: [], ‘sid’: ‘a2c4e03b4a2b4f7f8936e5a9d0f8b6a3’}
a2c4e03b4a2b4f7f8936e5a9d0f8b6a3: Sending packet MESSAGE data 0
Invalid session a2c4e03b4a2b4f7f8936e5a9d0f8b6a3
Invalid session a2c4e03b4a2b4f7f8936e5a9d0f8b6a3
Invalid session a2c4e03b4a2b4f7f8936e5a9d0f8b6a3
9aa12ae09a4840d49f45192530d91c17: Sending packet OPEN data {‘pingInterval’: 25000, 。。。。。。
‘pingTimeout’: 60000, ‘upgrades’: [], ‘sid’: ‘384dad37414646bf902390c113f37522’}
384dad37414646bf902390c113f37522: Sending packet MESSAGE data 0
Invalid session 384dad37414646bf902390c113f37522
Invalid session 384dad37414646bf902390c113f37522
384dad37414646bf902390c113f37522: Received packet CLOSE data None

Flask-SocketIO  Received packet CLOSE data None

Flask-SocketIO  Sending packet MESSAGE data 0

Session disconnected causes the thread handling eventlet + redis packets/events · Issue #263 · miguelgrinberg/Flask-SocketIO · GitHub

Failing to upgrade to websocket connection · Issue #175 · miguelgrinberg/Flask-SocketIO · GitHub

搜:

Invalid session

 去:

    socketio.run(app, debug=True)

改为:

    socketio.run(app)

好像问题依旧。

Flask-SocketIO  Invalid session

Invalid session Sending packet MESSAGE data 0

Invalid session · Issue #29 · miguelgrinberg/python-engineio · GitHub

提到:

The invalid session indicates that the server process received a Socket.IO request with a session it does not know about.

Possible causes I can think of:

you have multiple gevent servers. If server #1 creates the session, but then server #2 receives a subsequent request you will get this message. The solution is to either ensure multiple servers are handled with sticky sessions, or else use a single server.

your log does not show the times of each message. A session is removed after 60 seconds of inactivity (the value of the "pingTimeout" argument returned in the open packet).

但是不知道如何去:

use a single server

好像就是:

(此处的gunicorn的)worker设置为1个?

log中,好像也是开启了多个线程:

[2016-10-28 20:05:43 +0000] [1673] [INFO] Listening at: http://0.0.0.0:21085 (1673)
[2016-10-28 20:05:43 +0000] [1673] [INFO] Using worker: sync
[2016-10-28 20:05:43 +0000] [1682] [INFO] Booting worker with pid: 1682
[2016-10-28 20:05:43 +0000] [1683] [INFO] Booting worker with pid: 1683
[2016-10-28 20:05:43 +0000] [1684] [INFO] Booting worker with pid: 1684
[2016-10-28 20:05:43 +0000] [1685] [INFO] Booting worker with pid: 1685
Server initialized for gevent.
Server initialized for gevent.
Server initialized for gevent.
Server initialized for gevent.
。。。

或许就是意味着:

开了多个server?

那去试试:

把:

/Users/crifan/dev/dev_root/daryun/Projects/RunningFast/sourcecode/RunningFast-Server/supervisor.conf

中的:

[program:runningfast_stable]
command=/root/Envs/RunningFast/bin/gunicorn -w 4 -b 0.0.0.0:21084 run:app &

的worker改为1个:

command=/root/Envs/RunningFast/bin/gunicorn -w 1 -b 0.0.0.0:21084 run:app &

然后再去试试

-》

但是这样不就是:

少了worker

以后处理HTTP请求,性能就差了吗?

结果去

supervisorctl -c supervisor.conf restart runningfast_staging

结果还是有4个进程:

[2016-10-28 20:33:44 +0000] [2877] [INFO] Starting gunicorn 19.6.0
[2016-10-28 20:33:44 +0000] [2877] [INFO] Listening at: http://0.0.0.0:21085 (2877)
[2016-10-28 20:33:44 +0000] [2877] [INFO] Using worker: sync
[2016-10-28 20:33:44 +0000] [2886] [INFO] Booting worker with pid: 2886
[2016-10-28 20:33:44 +0000] [2887] [INFO] Booting worker with pid: 2887
[2016-10-28 20:33:44 +0000] [2892] [INFO] Booting worker with pid: 2892
[2016-10-28 20:33:44 +0000] [2893] [INFO] Booting worker with pid: 2893

干脆去命令行执行:

(RunningFast) ➜  staging gunicorn -w 1 -b 127.0.0.1:21084 run:app &
[1] 2910
(RunningFast) ➜  staging [2016-10-28 20:34:43 +0000] [2910] [INFO] Starting gunicorn 19.6.0
[2016-10-28 20:34:44 +0000] [2910] [INFO] Listening at: http://127.0.0.1:21084 (2910)
[2016-10-28 20:34:44 +0000] [2910] [INFO] Using worker: sync
[2016-10-28 20:34:44 +0000] [2919] [INFO] Booting worker with pid: 2919
Server initialized for gevent.

<div–<——————————————————————————

DEBUG in app [/root/RunningFast/staging/runningfast/app.py:82]:
app=<Flask ‘runningfast.app’>, xxx socketio=<flask_socketio.SocketIO object at 0x7efebafeb7d0>

结果问题依旧:

Invalid session flask  socketio

python – Using eventlet to manage socketio in Flask – Stack Overflow

Socketio swift + flask-socketio connect problems · Issue #154 · socketio/socket.io-client-swift · GitHub

Invalid session flask-socketio socket.io.min.js  GET  400 (BAD REQUEST)

去试试:

socketio = SocketIO(app, logger=True, engineio_logger=True)

此处,好像某一次,emit生效了,可以收到emit的内容了:

去看看,其中对应的log内容:

Connected socket IO

<div–<——————————————————————————

emitting event "my response" to 392ed7ab107345cbb0e2e16fca997e3c [/test]
392ed7ab107345cbb0e2e16fca997e3c: Sending packet MESSAGE data 2/test,["my response",{"data":"Connected socket IO"}]
392ed7ab107345cbb0e2e16fca997e3c: Sending packet MESSAGE data 0/test
392ed7ab107345cbb0e2e16fca997e3c: Received packet MESSAGE data 2/test,["my event",{"data":"abc"}]
received event "my event" from 392ed7ab107345cbb0e2e16fca997e3c [/test]

<div–<——————————————————————————

DEBUG in app [/root/RunningFast/staging/runningfast/app.py:179]:
my event, message={u’data’: u’abc’}

<div–<——————————————————————————

emitting event "my response" to 392ed7ab107345cbb0e2e16fca997e3c [/test]
392ed7ab107345cbb0e2e16fca997e3c: Sending packet MESSAGE data 2/test,["my response",{"data":"abc"}]
392ed7ab107345cbb0e2e16fca997e3c: Received packet MESSAGE data 2/test,["my event",{"data":"abc"}]
received event "my event" from 392ed7ab107345cbb0e2e16fca997e3c [/test]

<div–<——————————————————————————

DEBUG in app [/root/RunningFast/staging/runningfast/app.py:179]:
my event, message={u’data’: u’abc’}

<div–<——————————————————————————

emitting event "my response" to 392ed7ab107345cbb0e2e16fca997e3c [/test]
392ed7ab107345cbb0e2e16fca997e3c: Sending packet MESSAGE data 2/test,["my response",{"data":"abc"}]

以为解决了问题。

但是再去多测试几次,后来还是经常出错:

socket.io.min.js:1 GET http://115.29.173.126:21085/socket.io/?EIO=3&transport=polling&t=1477659042526-55&sid=f77a853342224c3e88da9bf305a8ccc5 net::ERR_EMPTY_RESPONSE

把最新的19.6的gunicorn去换成gunicorn 18.0

再去单独运行:

gunicorn -w 1 -b 127.0.0.1:21085 run:app &

结果又出现其他错误:

【未解决】把gunicorn从19.6降级为18.0后supervisorctl运行出错:gunicorn: error: No application module specified

通过手动去运行后,结果错误依旧:

(RunningFast) ➜  staging 2016-10-28 21:07:40 [3762] [CRITICAL] WORKER TIMEOUT (pid:3771)
2016-10-28 21:07:40 [3762] [CRITICAL] WORKER TIMEOUT (pid:3771)
2016-10-28 21:07:40 [3822] [INFO] Booting worker with pid: 3822
Server initialized for gevent.
…….
Invalid session d67a54dd26004ea4a2ebecf2bdcdd148
Invalid session d67a54dd26004ea4a2ebecf2bdcdd148
33ea9b653203412d86001943c6a8347a: Sending packet OPEN data {‘pingInterval’: 25000, ‘pingTimeout’: 60000, ‘upgrades’: [], ‘sid’: ’33ea9b653203412d86001943c6a8347a’}
33ea9b653203412d86001943c6a8347a: Sending packet MESSAGE data 0
33ea9b653203412d86001943c6a8347a: Received packet MESSAGE data 0/test

<div–<——————————————————————————

DEBUG in app [/root/RunningFast/staging/runningfast/app.py:189]:
Connected socket IO

<div–<——————————————————————————

emitting event "my response" to 33ea9b653203412d86001943c6a8347a [/test]
33ea9b653203412d86001943c6a8347a: Sending packet MESSAGE data 2/test,["my response",{"data":"Connected socket IO"}]
33ea9b653203412d86001943c6a8347a: Sending packet MESSAGE data 0/test

然后错误依旧:

所以还是换回最新的19.6的gunicorn:

  Successfully uninstalled gunicorn-18.0
(RunningFast) ➜  staging pip install gunicorn
Collecting gunicorn
  Using cached gunicorn-19.6.0-py2.py3-none-any.whl
Installing collected packages: gunicorn
Successfully installed gunicorn-19.6.0

把:

    <script type="text/javascript" src="//code.jquery.com/jquery-1.4.2.min.js"></script>

改为:

python – 使用flask-socketIO怎样讲json格式数据从服务器推送到前端? – SegmentFault

https://segmentfault.com/q/1010000004542668

中的,更新版本的js,试试:

<script type="text/javascript" src="//code.jquery.com/jquery-1.7.1.min.js"></script>

再加上:

    function a(){
        socket.emit(‘my event’, {data: ‘I\’m connected!’})
    };
    setTimeout(a, 3000);

然后:

<!DOCTYPE HTML>
<html>
<head>
    <title>Flask-SocketIO Test</title>
    <script type="text/javascript" src="//code.jquery.com/jquery-1.7.1.min.js"></script>
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.6/socket.io.min.js"></script>
<script>
$(document).ready(function(){
    var socket = io.connect(‘http://’ + document.domain + ‘:’ + location.port + ‘/test’);
    socket.on(‘my response’, function(msg) {
        $(‘#log’).append(‘<p>Received: ‘ + msg.data + ‘</p>’);
    });
    $(‘form#emit’).submit(function(event) {
        socket.emit(‘my event’, {data: $(‘#emit_data’).val()});
        return false;
    });
    $(‘form#broadcast’).submit(function(event) {
        socket.emit(‘my broadcast event’, {data: $(‘#broadcast_data’).val()});
        return false;
    });
    function a(){
        socket.emit(‘my event’, {data: ‘I\’m connected!’})
    };
    setTimeout(a, 3000);
});
</script>
</head>
<body>
    <h1>Flask-SocketIO Test</h1>
    <h2>Send:</h2>
    <table>
        <tr>
            <td>
                <form id="emit" method=’POST’ action=’#’>
                    <textarea name="emit_data" id="emit_data"></textarea>
                    <div><input type="submit" value="Emit"></div>
                </form>
            </td>
            <td>
                <form id="broadcast" method=’POST’ action=’#’>
                    <textarea name="broadcast_data" id="broadcast_data"></textarea>
                    <div><input type="submit" value="Broadcast"></div>
                </form>
            </td>
        </tr>
    </table>
    <h2>Receive:</h2>
    <div id="log"></div>
</body>
</html>

是可以输出对应的内容的:

但是其它部分还是一直出错。

【总结】

此处,尝试了各种办法,但是都还是无法解决此处的:

js+html去测试Flask-SocketIO时,所输出的错误:

socket.io.min.js:1 POST 400 (BAD REQUEST)

转载请注明:在路上 » 【未解决】Flask-SocketIO测试出错:POST socket.io EIO 400 BAD REQUEST

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.222 seconds, using 22.23MB memory