我正在尝试使用Kenneth reitz的 Flask-Sockets库来编写一个简单的websocket接口/服务器.这是我到目前为止所拥有的.
from flask import Flask
from flask_sockets import Sockets
app = Flask(__name__)
sockets = Sockets(app)
@sockets.route('/echo')
def echo_socket(ws):
while True:
message = ws.receive()
ws.send(message)
@app.route('/')
def hello():
return \
'''
<html>
<head>
<title>Admin</title>
<script type="text/javascript">
var ws = new WebSocket("ws://" + location.host + "/echo");
ws.onmessage = function(evt){
var received_msg = evt.data;
alert(received_msg);
};
ws.onopen = function(){
ws.send("hello john");
};
</script>
</head>
<body>
<p>hello world</p>
</body>
</html>
'''
if __name__ == "__main__":
app.run(debug=True)
Run Code Online (Sandbox Code Playgroud)
我期待发生的是当我进入默认的烧瓶页面时,http://localhost:5000在我的情况下,我会看到一个包含文本的警告框hello john …
我正在尝试使用Flask-Sockets和示例代码:
sockets = Sockets(app)
@sockets.route('/echo')
def echo_socket(ws):
while True:
message = ws.receive()
ws.send(message)
Run Code Online (Sandbox Code Playgroud)
不幸的是,当我使用我的浏览器转到url/echo时,它给出了一个错误说:
File "/Library/Python/2.7/site-packages/Flask-0.10-py2.7.egg/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/Library/Python/2.7/site-packages/flask_sockets.py", line 37, in __call__
environment = environ['wsgi.websocket']
KeyError: 'wsgi.websocket'
Run Code Online (Sandbox Code Playgroud)
有人在想我做错了吗?欢迎所有提示!
[编辑] @jbub - 感谢您的提示!所以首先我现在使用gunicorn而不是内置的开发服务器.所以我开始使用它gunicorn -k flask_sockets.worker -b 0.0.0.0:5000 main:app.然后我将下面的代码插入到views.py中,其中echo_test.html是您提供的代码.当我现在访问/ echo_test时,我确实得到一个提示"套接字关闭".
sockets = Sockets(app)
@sockets.route('/echo')
def echo_socket(ws):
while True:
message = ws.receive()
ws.send(message)
@app.route('/echo_test', methods=['GET'])
def echo_test():
return render_template('echo_test.html')
Run Code Online (Sandbox Code Playgroud)
但是,假设我的目标是在页面上有一个单词(从列表中随机选择),该单词将从列表中随机选择的其他值进行更新.你有任何关于实现这一目标的提示吗?
我有一个现有的应用程序在nginx后面运行uwsgi/flask.我想将websockets集成到同一个应用程序中.Flask插座看起来很有前景,所以我正在尝试.
Flask套接字简要提到运行gevent-websocket,但没有提供示例代码来将现有应用程序集成到此设置中.我也尝试了几种gevent-websocket与我现有的烧瓶应用程序的组合,但我仍然保持500秒.另外,如果我能找到500的本质,那将是很好的,但错误不会出现在uwsgi日志中,就像它们对现有的烧瓶应用程序一样.所以,如果你有任何调试下降来显示一个tracedump,那就太好了.
任何人都可以帮我一个简单的回声路线使用这个设置吗?提前致谢.
仅供参考,我目前正在使用:
我正在使用flask_sockets库构建一个应用程序.我该如何测试?
这是我想编写单元测试的代码示例:
import flask
from flask import Flask
from flask_sockets import Sockets
app = Flask(__name__)
sockets = Sockets(app)
ws_conns = []
@sockets.route('/echo')
def echo_socket(ws):
#on connect
ws_conns.append(ws)
#while connected
while True:
# message = ws.receive()
# if message is None:
# #socket has closed/errored
# break
for c in ws_conns:
c.send('hello and goodbye!')
#disconnected
ws_conns.remove(ws)
ws.close()
Run Code Online (Sandbox Code Playgroud)
我在这个git repo中使用了这段代码.
我正在使用 Nginx + Flask-socketio + aws elb,当在 https 上加载 URL 时,我收到以下错误消息,该消息与 Nginx 和套接字相关,请对此提供帮助,
socket.io.min.js:2 Mixed Content: The page at 'https://localhost/' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://localhost/socket.io/1/?t=1477375737508'. This request has been blocked; the content must be served over HTTPS.d.handshake @ socket.io.min.js:2
socket.io.min.js:2 XMLHttpRequest cannot load http://localhost/socket.io/1/?t=1477375737508. Failed to start loading.
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Flask-socketio 运行我的 Flask 应用程序。在更改了许多配置之后,我终于成功地在 aws 上的 ubuntu 服务器上使用 Gunicorn、nginx 和 gevent 运行我的 Flask 应用程序。但每当我尝试连接时,连接都会失败,并且在错误日志中出现“无效会话”错误。我也安装了 gevent-websockets 但它也不起作用。这是错误消息。
[29/06/2020 03:50:28.388|15022|WARNING|engineio.server |server.py:391 handle_request ]: Invalid session 90ff42cdbd944b07ad2c2f6f484ff5a3
[29/Jun/2020:03:50:28 +0500] 127.0.0.1 "POST /socket.io/?EIO=3&transport=polling&t=NBz7-Tz&sid=90ff42cdbd944b07ad2c2f6f484ff5a3 HTTP/1.1" 400 11 "-" "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36"
Run Code Online (Sandbox Code Playgroud)
这是我的 nginx 配置。
server {
listen 80;
server_name my_app.com;
location / {
proxy_pass "http://localhost:5000";
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
fastcgi_read_timeout 300s;
proxy_read_timeout 300;
}
location /static {
alias /opt/deployment/my-api-app/static/;
}
error_log …Run Code Online (Sandbox Code Playgroud) 有人可以解释一下 socket、socketio、flask socketio 与 python 的区别吗?为了使用这个带有颤动的套接字,我应该在后端编写什么代码?就像我应该写服务器和客户端还是只写客户端?
我正在尝试制作一个使用 WebSockets 的 Flask 应用程序。Flask-sockets 中的示例有效,但我如何从常规视图发送消息?
.emit()与 Flask-SocketIO 如何使用和方法类似.send()。
在下面的示例中(来自 Flask-Sockets 示例),我希望能够从 -view 广播消息hello。
from flask import Flask
from flask_sockets import Sockets
app = Flask(__name__)
sockets = Sockets(app)
@sockets.route('/echo')
def echo_socket(ws):
while not ws.closed:
message = ws.receive()
ws.send(message)
@app.route('/')
def hello():
# How can I send a WebSocket message from here?
return 'Hello World!'
if __name__ == "__main__":
from gevent import pywsgi
from geventwebsocket.handler import WebSocketHandler
server = pywsgi.WSGIServer(('', 5000), app, handler_class=WebSocketHandler)
server.serve_forever()
Run Code Online (Sandbox Code Playgroud)