Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1#!/usr/bin/env python32#3# Copyright (c) The Bitcoin Core developers4# Distributed under the MIT software license, see the accompanying5# file COPYING or https://opensource.org/license/mit/.7import sys8import time9import urllib.request12def download_from_url(url, archive):13print(f"Fetching: {url}")14last_print_time = time.time()16def progress_hook(progress_bytes, total_size):17nonlocal last_print_time18now = time.time()19percent = min(100, (progress_bytes * 100) / total_size)20bar_length = 4021filled_length = int(bar_length * percent / 100)22bar = '#' * filled_length + '-' * (bar_length - filled_length)23if now - last_print_time >= 1 or percent >= 100:24print(f'\rDownloading: [{bar}] {percent:.1f}%', flush=True, end="")25last_print_time = now27with urllib.request.urlopen(url) as response:28if response.status != 200:29raise RuntimeError(f"HTTP request failed with status code: {response.status}")31sock_info = response.fp.raw._sock.getpeername()32print(f"Connected to {sock_info[0]}")34total_size = int(response.getheader("Content-Length"))35progress_bytes = 037with open(archive, 'wb') as file:38while True:39chunk = response.read(8192)40if not chunk:41break42file.write(chunk)43progress_bytes += len(chunk)44progress_hook(progress_bytes, total_size)46if progress_bytes < total_size:47raise RuntimeError(f"Download incomplete: expected {total_size} bytes, got {progress_bytes} bytes")49print('\n', flush=True, end="") # Flush to avoid error output on the same line.52def download_script_assets(script_assets_dir):53script_assets_dir.mkdir(parents=True, exist_ok=True)54script_assets = script_assets_dir / "script_assets_test.json"55url = "https://github.com/bitcoin-core/qa-assets/raw/main/unit_test_data/script_assets_test.json"56try:57download_from_url(url, script_assets)58except Exception as e:59print(f"\nDownload failed: {e}", file=sys.stderr)60print("Retrying download after failure ...", file=sys.stderr)61time.sleep(12)62try:63download_from_url(url, script_assets)64except Exception as e2:65sys.exit(e2)
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.