用Chrome的ws插件:
Simple WebSocket Client
去测试打开ws端口:
ws://115.29.173.126:21084/users/user-17681082-13ae-4a0c-a8bb-83f5814a1b22/p7Isn6ZJkj6Ot7mXAM2melDGT9imrhI9
但是出错:
WebSocket connection to ‘ws://115.29.173.126:21084/users/user-17681082-13ae-4a0c-a8bb-83f5814a1b22/p7Isn6ZJkj6Ot7mXAM2melDGT9imrhI9’ failed: Error during WebSocket handshake: Unexpected response code: 404
弹出对话框:
Simple WebSocket Client
undefined
log为:
index.js:15 WebSocket connection to ‘ws://115.29.173.126:21084/users/user-17681082-13ae-4a0c-a8bb-83f5814a1b22/p7Isn6ZJkj6Ot7mXAM2melDGT9imrhI9’ failed: Error during WebSocket handshake: Unexpected response code: 404 open @ index.js:15(anonymous function) @ index.js:95handle @ jquery-1.4.3.min.js:63n @ jquery-1.4.3.min.js:56 index.js:55 CLOSED: ws://115.29.173.126:21084/users/user-17681082-13ae-4a0c-a8bb-83f5814a1b22/p7Isn6ZJkj6Ot7mXAM2melDGT9imrhI9 |
不过访问同一服务器中另外的一个端口不同的(同样的代码的)Flask的app,则是可以正常打开websocket的:
OPENED: ws://115.29.173.126:21085/users/user-17681082-13ae-4a0c-a8bb-83f5814a1b22/IYdRjFnYdVjDkGIgxex1fVcJF7HkQ4tW index.js:29 CLOSING … index.js:55 CLOSED: ws://115.29.173.126:21085/users/user-17681082-13ae-4a0c-a8bb-83f5814a1b22/IYdRjFnYdVjDkGIgxex1fVcJF7HkQ4tW |
对应的Flask的app是通过gunicorn和supervisor启动的:
supervisorctl -c supervisor.conf restart runningfast_staging
supervisorctl -c supervisor.conf restart runningfast_stable
配置是:
[program:runningfast_stable] directory=/root/RunningFast/stable command=/root/Envs/RunningFast/bin/gunicorn -w 4 -b 0.0.0.0:21084 -k flask_sockets.worker run:app & startsecs=0 stopwaitsecs=0 autostart=false autorestart=false stdout_logfile_maxbytes=1MB stderr_logfile_maxbytes=1MB stdout_logfile=/root/RunningFast/stable/logs/gunicorn.log stderr_logfile=/root/RunningFast/stable/logs/gunicorn.err [program:runningfast_staging] directory=/root/RunningFast/staging command=/root/Envs/RunningFast/bin/gunicorn -w 4 -b 0.0.0.0:21085 -k flask_sockets.worker run:app & startsecs=0 stopwaitsecs=0 autostart=false autorestart=false stdout_logfile_maxbytes=1MB stderr_logfile_maxbytes=1MB stdout_logfile=/root/RunningFast/staging/logs/gunicorn.log stderr_logfile=/root/RunningFast/staging/logs/gunicorn.err |
即:
command=/root/Envs/RunningFast/bin/gunicorn -w 4 -b 0.0.0.0:21084 -k flask_sockets.worker run:app &
和
command=/root/Envs/RunningFast/bin/gunicorn -w 4 -b 0.0.0.0:21085 -k flask_sockets.worker run:app &
21085端口的ws是好的。
而
21084的端口的ws是有问题的。
1.难道是因为:
同一个Flask的服务器中,只能运行单个ws?
或许只能是:
最后一个启动的flask的app的ws可以使用?
那么去试试:
先:
supervisorctl -c supervisor.conf restart runningfast_staging
再:
supervisorctl -c supervisor.conf restart runningfast_stable
看看是不是:
stable的21084可用
staging的21085不可用
(RunningFast) ➜ staging supervisorctl -c supervisor.conf restart runningfast_staging runningfast_staging: stopped runningfast_staging: started (RunningFast) ➜ staging cd ../stable (RunningFast) ➜ stable supervisorctl -c supervisor.conf restart runningfast_stable runningfast_stable: stopped runningfast_stable: started |
结果还是21084不可用。。。
2.搜:
flask-sockets Error during WebSocket handshake Unexpected response code 404
flask-sockets Unexpected response code 404
ws websocket Unexpected response code 404
ws Flask websocket Unexpected response code 404
“well, error 404 normally means that the endpoint wasn’t found. “
->404意味着端口没有找到
-》就是不存在,没有配置好
-》再去看看自己的配置,是否可能存在哪些问题
有空去试试:
去停止掉21804和21805这两个Flask的app
只运行21804的去试试:
/root/Envs/RunningFast/bin/gunicorn -w 4 -b 0.0.0.0:21084 -k flask_sockets.worker run:app &
Flask websocket code 404
感觉好像和我这里错误有点类似或者有关系。
但是此处没法解决啊
python – Trouble deploying a Flask Websockets App on Heroku – Stack Overflow
draft-hixie-thewebsocketprotocol-55 – The WebSocket protocol
“The Web Socket protocol
draft-hixie-thewebsocketprotocol-55
Abstract
The Web Sockets protocol enables two-way communication between a user
agent running untrusted code running in a controlled environment to a
remote host that has opted-in to communications from that code. The
security model used for this is the Origin-based security model
commonly used by Web browsers. The protocol consists of an initial
handshake followed by basic message framing, layered over TCP. The
goal of this technology is to provide a mechanism for browser-based
applications that need two-way communication with servers that does
not rely on opening multiple HTTP connections (e.g. using
XMLHttpRequest or <iframe>s and long polling).
“
想办法去给Socket输出log
看看有没有对应的配置参数。
kennethreitz/flask-sockets: Elegant WebSockets for your Flask apps.
flask-sockets not work
flask-sockets two app
->
WebSocket support — uWSGI 2.0 documentation
python – flask-sockets and ZeroMQ – Stack Overflow
flask-sockets Using worker flask_sockets.worker
Issue with flask-sockets’s concurrent connection served by GUnicorn – Q&A Overflow
->
winkidney/flask-sockets-tornado: webscoket in flask with tornado style : )
->
ulimit – How do I increase the open files limit for a non-root user? – Ask Ubuntu
Gunicorn worker-connections
Settings — Gunicorn 19.6.0 documentation
“
worker_class
-k STRING, –worker-class STRING
sync
The type of workers to use.
The default class (sync) should handle most “normal” types of workloads. You’ll want to read Design for information on when you might want to choose one of the other worker classes.
A string referring to one of the following bundled classes:
sync
eventlet – Requires eventlet >= 0.9.7
gevent – Requires gevent >= 0.13
tornado – Requires tornado >= 0.2
gthread – Python 2 requires the futures package to be installed
gaiohttp – Requires Python 3.4 and aiohttp >= 0.21.5
Optionally, you can provide your own worker by giving Gunicorn a Python path to a subclass of gunicorn.workers.base.Worker. This alternative syntax will load the gevent class: gunicorn.workers.ggevent.GeventWorker. Alternatively, the syntax can also load the gevent class with egg:gunicorn#gevent.
worker_connections
–worker-connections INT
1000
The maximum number of simultaneous clients.
This setting only affects the Eventlet and Gevent worker types.“
flask sockets 404
Recently Active ‘flask-sockets’ Questions – Stack Overflow
WebSocket Security | Heroku Dev Center
然后去:
重新改了端口配置,从21084改为21086:
directory=/root/RunningFast/stable[program:runningfast_stable] directory=/root/RunningFast/stable command=/root/Envs/RunningFast/bin/gunicorn -w 4 -b 0.0.0.0:21086 -k flask_sockets.worker run:app & |
再去重载配置,再去运行:
(RunningFast) ➜ stable supervisorctl -c supervisor.conf restart runningfast_stable runningfast_stable: ERROR (not running) runningfast_stable: started (RunningFast) ➜ stable supervisorctl -c supervisor.conf restart runningfast_stable runningfast_stable: stopped runningfast_stable: started (RunningFast) ➜ stable supervisorctl -c supervisor.conf status runningfast_stable runningfast_stable RUNNING pid 24252, uptime 0:00:20 |
结果就可以打开新的21086的ws端口了:
-》推测之前的配置有误?
还是21084被其他进程,程序占用了?
重新改回来21084端口:
command=/root/Envs/RunningFast/bin/gunicorn -w 4 -b 0.0.0.0:21084 -k flask_sockets.worker run:app & |
重载配置:
(RunningFast) ➜ stable supervisorctl -c supervisor.conf reload Restarted supervisord (RunningFast) ➜ stable supervisorctl -c supervisor.conf restart runningfast_stable runningfast_stable: ERROR (not running) runningfast_stable: started |
再试试
果然就可以了。
【总结】
此处,对于同样的一套Flask的app,
之所以21085的端口的ws可以正常访问,
而21084端口的ws无法访问
原因是:
估计是之前不知道哪次,把21084的配置改出问题了。但是没有及时改回来。
所以有问题。
重新确认配置:
附上完整的配置:
[program:runningfast_stable] directory=/root/RunningFast/stable command=/root/Envs/RunningFast/bin/gunicorn -w 4 -b 0.0.0.0:21084 -k flask_sockets.worker run:app & startsecs=0 stopwaitsecs=0 autostart=false autorestart=false stdout_logfile_maxbytes=1MB stderr_logfile_maxbytes=1MB stdout_logfile=/root/RunningFast/stable/logs/gunicorn.log stderr_logfile=/root/RunningFast/stable/logs/gunicorn.err [program:runningfast_staging] directory=/root/RunningFast/staging command=/root/Envs/RunningFast/bin/gunicorn -w 4 -b 0.0.0.0:21085 -k flask_sockets.worker run:app & startsecs=0 stopwaitsecs=0 autostart=false autorestart=false stdout_logfile_maxbytes=1MB stderr_logfile_maxbytes=1MB stdout_logfile=/root/RunningFast/staging/logs/gunicorn.log stderr_logfile=/root/RunningFast/staging/logs/gunicorn.err |
再去reload再运行:
(RunningFast) ➜ stable supervisorctl -c supervisor.conf reload Restarted supervisord (RunningFast) ➜ stable supervisorctl -c supervisor.conf restart runningfast_stable runningfast_stable: ERROR (not running) runningfast_stable: started (RunningFast) ➜ stable supervisorctl -c supervisor.conf restart runningfast_stable runningfast_stable: stopped runningfast_stable: started |
然后21084端口的ws就正常工作了:
转载请注明:在路上 » 【已解决】Flask-Sockets的websocket访问出错:Error during WebSocket handshake: Unexpected response code 404