我正在尝试设置一个需要WebSocket连接的Web应用程序,并且应该将本地mp4文件流式传输到其客户端。
我正在使用Flask 1.0.2,Flask-SocketIO 3.0.2和Eventlet 0.24.1来完成这项工作,我在flask-socketio docs上读到,如果您使用socketio.run(app)
启动Flask应用,应该具有可用于生产环境的Web服务器。
因此,我设置了一个最小的服务器,该服务器应提供mp4文件,并提供一个应显示它们的html页面-可以工作,但是每次客户端发送206 HTTP请求是因为有人寻求视频,此错误就会在Python控制台上出现:
Traceback (most recent call last):
File "C:\Users\Fabrizio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\eventlet\hubs\selects.py", line 55, in wait
listeners.get(fileno, noop).cb(fileno)
File "C:\Users\Fabrizio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\eventlet\greenthread.py", line 219, in main
result = function(*args, **kwargs)
File "C:\Users\Fabrizio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\eventlet\wsgi.py", line 789, in process_request
proto.__init__(conn_state, self)
File "C:\Users\Fabrizio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\eventlet\wsgi.py", line 345, in __init__
self.finish()
File "C:\Users\Fabrizio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\eventlet\wsgi.py", line 703, in finish
BaseHTTPServer.BaseHTTPRequestHandler.finish(self)
File "C:\Users\Fabrizio\AppData\Local\Programs\Python\Python37-32\lib\socketserver.py", line 781, in finish
self.wfile.close()
File "C:\Users\Fabrizio\AppData\Local\Programs\Python\Python37-32\lib\socket.py", line 607, in write
return self._sock.send(b)
File "C:\Users\Fabrizio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\eventlet\greenio\base.py", line 395, in send
return self._send_loop(self.fd.send, data, flags)
File "C:\Users\Fabrizio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\eventlet\greenio\base.py", line 382, in _send_loop
return send_method(data, *args)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine.
Removing descriptor: 920
此错误不会中断服务器的执行,也不会引起任何问题,但是它会用这些堆栈跟踪填充控制台,这确实很烦人。这是项目结构:
/测试:
| ____ a.mp4
| ____ index.html
| ____ test.py
a.mp4是一个示例视频,要重现此错误,它应该很长,因为否则文件将立即加载而不会发送部分请求。
index.html
<video width="720" src="http://127.0.0.1:5000/a.mp4" controls></video>
test.py
from flask import Flask, send_from_directory
from flask_socketio import SocketIO
app = Flask(__name__)
socketio = SocketIO(app)
@app.route("/")
def index():
return send_from_directory("","index.html")
@app.route("/a.mp4")
def video():
return send_from_directory("","a.mp4")
if __name__ == "__main__":
socketio.run(app)
是否有一种方法可以捕获这些错误,或者可能在不使用其他http服务器的情况下进行修复?