import time import json from flask import Flask, render_template, request from flask_sock import Sock app = Flask(__name__) sock = Sock(app) VERSION = '1.0.21' @app.route('/') def index(): return render_template('index.html', version=VERSION) @app.route('/health') def health(): return {'ok': True, 'version': VERSION} @sock.route('/ws-upload') def ws_upload(ws): """WebSocket: получает чанки, ACK после каждого""" t0 = time.time() total = 0 while True: chunk = ws.receive() if chunk is None: break if isinstance(chunk, str) and chunk == 'DONE': break total += len(chunk) ws.send(json.dumps({'ack': total})) elapsed = round((time.time() - t0) * 1000) ws.send(json.dumps({'ok': True, 'bytes': total, 'elapsed_ms': elapsed})) @app.route('/upload', methods=['POST']) def upload(): t0 = time.time() # raw-режим: тело как есть (клиент шлёт файл напрямую через xhr.send(file)) raw = request.get_data() total = len(raw) elapsed = round((time.time() - t0) * 1000) return {'ok': True, 'bytes': total, 'elapsed_ms': elapsed} if __name__ == '__main__': import subprocess, sys subprocess.run([sys.executable, '-m', 'gunicorn', 'app:app', '--bind', '0.0.0.0:5000', '--workers', '2', '--threads', '4', '--timeout', '300', '--keep-alive', '5'])