Files
2026-07-11 10:42:31 +04:00

56 lines
1.5 KiB
Python

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.25'
@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: получает чанки, считает байты и время"""
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)
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',
'--worker-class', 'gevent',
'--workers', '1',
'--worker-connections', '1000',
'--timeout', '300'])