我正在构建使用套接字(flask socketio)的python Flask应用程序。基本上,客户端会向服务器发送一些他想要执行的命令。服务器将执行命令并将套接字发送到客户端命令输出。
有接收用户请求的处理函数。此函数将执行命令并将大量套接字发送回客户端。还使用了获取 stdout 和 stderr 线程。
@socket.on ('run-code')
@authenticated_only
def socket_run_code_request (request):
# run command
# emit socket for each line of output
Run Code Online (Sandbox Code Playgroud)
如果flask app 处于调试模式,函数内部发出的套接字将在函数结束之前到达客户端(这是可取的)。但是如果调试关闭,所有套接字都会以某种方式排队并在函数结束后发送。没有来自服务器的实时响应,只是
单击执行->稍等->这是您的输出
代替:
单击执行->这是一些输出->这是另一行-> ...
我读过瓶文档,但说明调试是以下:
如果您启用调试支持,服务器将在代码更改时重新加载自己,如果出现问题,它还将为您提供有用的调试器
有没有办法告诉 Flask 立即发送所有内容,或者一些如何解决这个问题的想法?它可能与flask的flask-socketio插件有关
非常感谢您的反馈:)
我一直在关注本教程,尝试Flask SocketIO使用nginx和运行gunicorn.
nginx的
server {
location / {
proxy_pass http://127.0.0.1:8000;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /socket.io {
proxy_pass http://localhost:8000/socket.io;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
}
Run Code Online (Sandbox Code Playgroud)
gunicorn_config.py
bind = '127.0.0.1:8000'
workers = 2
worker_class = 'socketio.sgunicorn.GeventSocketIOWorker'
Run Code Online (Sandbox Code Playgroud)
在Supervisor我把我的应用程序:
[program:gunicorn-couponmonk]
directory = ~/couponmonk_project
command =~/venv/py2.7/bin/python ~/venv/py2.7/bin/gunicorn -c ~/venv/py2.7/lib/python2.7/site-packages/gunicorn/gunicorn_config.py __init__.py
stdout_logfile …Run Code Online (Sandbox Code Playgroud) 我要实现以下情形:
我正在使用Blocktrail API中的webhook来“监听”事件,即接收地址上的硬币。
现在,当事件发生时,API会对我的URL进行POST。这应该向通过socket.io连接到我的服务器的浏览器发送一条消息(例如“在区块链上看到付款”)
所以问题是
我如何使用flask-socketio将消息从路由发送到套接字
伪代码:
@app.route('/callback/<address>')
def callback(id):
socketio.send('payment seen on blockchain')
@socketio.on('address',address)
def socketlisten(address):
registerCallback(address)
Run Code Online (Sandbox Code Playgroud) 我正在尝试设置一个小型服务器来处理HTTP和socketio请求 - 我没有太多设置服务器的经验,但现在apache2服务http就好了.然而,socketio事务继续失败,错误代码为400(错误请求),我在服务器日志中看到一些奇怪的错误.有时我看到一个engineio错误,服务器响应"错误请求"和代码400,但总是它告诉我需要启动eventlet服务器:
[Mon Jan 11 19:02:54.068282 2016] [:error] [pid 4908:tid 140274923673344] [client 100.96.180.39:53473] return ws(environ, start_response)
[Mon Jan 11 19:02:54.068305 2016] [:error] [pid 4908:tid 140274923673344] [client 100.96.180.39:53473] File "/var/www/projectENV/lib/python2.7/site-packages/engineio/async_eventlet.py", line 10, in __call__
[Mon Jan 11 19:02:54.068342 2016] [:error] [pid 4908:tid 140274923673344] [client 100.96.180.39:53473] raise RuntimeError('You need to use the eventlet server.')
[Mon Jan 11 19:02:54.068380 2016] [:error] [pid 4908:tid 140274923673344] [client 100.96.180.39:53473] RuntimeError: You need to use the eventlet server. See the Deployment section of the documentation for …Run Code Online (Sandbox Code Playgroud) 我有一个非常标准的flask-socket.io应用程序:
server:eventlet我使用以下命令启动应用程序:socketio.run(app,host ='0.0.0.0')
经常但并不总是我有某种超时:
Traceback (most recent call last):
File "/projects/ici_chat_prototype01/env/lib/python3.5/site-packages/eventlet/wsgi.py", line 507, in handle_one_response
result = self.application(self.environ, start_response)
File "/projects/ici_chat_prototype01/env/lib/python3.5/site-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/projects/ici_chat_prototype01/env/lib/python3.5/site-packages/flask_socketio/__init__.py", line 42, in __call__
start_response)
File "/projects/ici_chat_prototype01/env/lib/python3.5/site-packages/engineio/middleware.py", line 47, in __call__
return self.engineio_app.handle_request(environ, start_response)
File "/projects/ici_chat_prototype01/env/lib/python3.5/site-packages/socketio/server.py", line 360, in handle_request
return self.eio.handle_request(environ, start_response)
File "/projects/ici_chat_prototype01/env/lib/python3.5/site-packages/engineio/server.py", line 267, in handle_request
environ, start_response)
File "/projects/ici_chat_prototype01/env/lib/python3.5/site-packages/engineio/socket.py", line 89, in handle_get_request
start_response)
File "/projects/ici_chat_prototype01/env/lib/python3.5/site-packages/engineio/socket.py", line 130, in _upgrade_websocket
return ws(environ, start_response)
File "/projects/ici_chat_prototype01/env/lib/python3.5/site-packages/engineio/async_eventlet.py", line …Run Code Online (Sandbox Code Playgroud) 我目前正在尝试了解套接字是如何工作的。我正在使用 Flask-socketio 和 python socketio 客户端并运行一个基本示例。这是我到目前为止所做的
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
@socketio.on('aaa')
def test_connect():
print("Welcome, aaa received")
emit('aaa_response', {'data': 'Server'})
if __name__ == '__main__':
socketio.run(app, port=8000)
Run Code Online (Sandbox Code Playgroud)
from socketIO_client import SocketIO, LoggingNamespace
def on_aaa_response(args):
print('on_aaa_response', args['data'])
socketIO = SocketIO('localhost', 8000, LoggingNamespace)
socketIO.on('aaa_response', on_aaa_response)
socketIO.emit('aaa')
socketIO.wait(seconds=1)
Run Code Online (Sandbox Code Playgroud)
运行client.py. 我确实看到了服务器打印"Welcome, aaa recived"。我不知道我在这里做错了什么,如果需要这里是我的日志
错误日志
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
app …Run Code Online (Sandbox Code Playgroud) 我正在使用flask-socketio从我的python web服务器到javascript客户端建立套接字连接.我能够建立连接但是它会在一段时间内(大约5秒左右)出现错误
socket.io.min.js:2与'ws:// localhost:5000/socket.io /?EIO = 3&transport = websocket&sid = 8ed663e14c6f47328b64f2d29a30d1cd'的WebSocket连接失败:收到包含无效UTF-8的破解关闭帧.
发送消息的服务器端代码(定期调用,每隔5秒调用一次)
def send_message(result):
# it will forward the message to all clients.
print("sending message")
socketio.send("Send working", json=False)
Run Code Online (Sandbox Code Playgroud)
客户端代码接收消息
socket.on('message', function (data) {
console.log('message form backend ' + data);
});
Run Code Online (Sandbox Code Playgroud)
它以某种方式打破了 - >然后暂时没有发生 - >然后再次自动连接 - >然后再次中断.
有人可以帮忙吗?非常感谢!
我正在构建一个在 Windows 中使用 Waitress、Flask 和 Flask_SocketIO 的 API。从命令行一切正常,但是当我尝试使用 cx_Freeze 将 API 捆绑到 .exe 中然后运行它时,出现错误:
ValueError: Invalid async_mode specified
Run Code Online (Sandbox Code Playgroud)
我能找到的关于这个问题的唯一信息是python-socketio github thread。为了解决这个问题,该线程中有很多好的建议可以尝试,但我已经尝试了每件事,但仍然得到相同的结果。
我怀疑我缺少一个模块,但我不知道如何具体找出缺少哪个模块。有人对尝试的事情有什么建议吗?即使它不能直接解决我的问题,了解如何深入堆栈跟踪以查找丢失的模块也会有所帮助。
我有一个可以将音频文件转换为文本的应用程序。使用烧瓶和烧瓶插座。当我使用“python run.py”运行它时,它工作得很好,但是当我使用“gunicorn -k eventlet -b 0.0.0.0:5000 run:app”运行它时,它将停止在调用 google 的部分audio.py 文件中的语音转文本 api。
这些是现在的当前代码。
运行.py:
from ats import socketio, app, db
if __name__ == '__main__':
db.create_all()
socketio.run(app, host='0.0.0.0', port=5001, debug=True)
Run Code Online (Sandbox Code Playgroud)
初始化.py
import logging, json
from flask import Flask, jsonify, render_template, request
from flask_socketio import SocketIO, emit, send
from flask_cors import CORS
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmall
app = Flask(__name__, instance_relative_config=True, static_folder="templates/static", template_folder="templates")
# Create db instance
db = SQLAlchemy(app)
ma = Marshmallow(app)
@app.route('/')
def index():
return render_template('index.html');
# …Run Code Online (Sandbox Code Playgroud) 我有一个内置在 Flask 中的 Web 应用程序,可以在其中捕获推文(使用 Tweepy 库)并显示在前端。我使用 Socket IO 在前端实时显示推文。
当我在本地运行它时,我的代码工作正常。推文立即出现。
但是,当我对 Web 应用程序进行 Docker 化时,前端不会立即更新。显示更改需要一些时间(有时我认为由于缓慢而丢失了推文)
以下是我网站的代码摘录:
fortsocket.js
$(document).ready(function () {
/************************************/
/*********** My Functions ***********/
/************************************/
function stream_active_setup() {
$("#favicon").attr("href", "/static/icons/fortnite-active.png");
$("#stream-status-ic").attr("src", "/static/icons/stream-active.png");
$("#stream-status-text").text("Live stream active");
}
function stream_inactive_setup() {
$("#favicon").attr("href", "/static/icons/fortnite-inactive.png");
$("#stream-status-ic").attr("src", "/static/icons/stream-inactive.png");
$("#stream-status-text").text("Live stream inactive");
}
/*********************************/
/*********** My Events ***********/
/*********************************/
// Socket connection to server
// Prometheus
//var socket = io.connect('http://104.131.173.145:8083');
// Local
var socket = io.connect(window.location.protocol + '//' + document.domain + ':' + location.port); …Run Code Online (Sandbox Code Playgroud)