# app.py from flask import Flask, Response, request, jsonify import os app = Flask(__name__) # —————————————————————————————— # Configuration: Test size = 1 GiB # —————————————————————————————— DOWNLOAD_SIZE_BYTES = 1 * 1024 * 1024 * 1024 # 1 GiB # —————————————————————————————— # Generator for streaming 1 GiB of random bytes in 10 MiB chunks # —————————————————————————————— def generate_random_blob(): sent = 0 chunk_size = 10 * 1024 * 1024 # 10 MiB per chunk while sent < DOWNLOAD_SIZE_BYTES: to_send = min(chunk_size, DOWNLOAD_SIZE_BYTES - sent) yield os.urandom(to_send) sent += to_send # —————————————————————————————— # Route: “/” – serve the HTML + JS client # —————————————————————————————— @app.route("/") def index(): html = """
This test will measure your real Internet speed by downloading 1 GiB of random data and uploading 1 GiB back to the server. All timing runs entirely in your browser.
""" return Response(html, mimetype="text/html") # —————————————————————————————— # Route: /ping_test – empty 200 response for ping # —————————————————————————————— @app.route("/ping_test") def ping_test(): return Response(status=200) # —————————————————————————————— # Route: /download_test – stream 1 GiB of random data # —————————————————————————————— @app.route("/download_test") def download_test(): headers = { "Content-Disposition": f"attachment; filename=\"test_{DOWNLOAD_SIZE_BYTES}.bin\"", "Content-Length": str(DOWNLOAD_SIZE_BYTES), "Cache-Control": "no-store" } return Response( generate_random_blob(), mimetype="application/octet-stream", headers=headers ) # —————————————————————————————— # Route: /upload_test – consume uploaded data in chunks and return JSON # —————————————————————————————— @app.route("/upload_test", methods=["POST"]) def upload_test(): total = 0 # Read the incoming stream in 1 MiB chunks and discard while True: chunk = request.stream.read(1 * 1024 * 1024) if not chunk: break total += len(chunk) # total should be near 1 GiB if upload completed return jsonify({"received_bytes": total}) # —————————————————————————————— # Run the Flask app on port 7860 # —————————————————————————————— if __name__ == "__main__": app.run(host="0.0.0.0", port=7860, debug=False)