Skip to content

Metadata Download

JackyHe398 on OldHome_main edited this page Aug 1, 2026 · 2 revisions

When starting with only an info hash, you can connect to a peer that supports ut_metadata and request the torrent metainfo. After assembling the pieces and verifying the info hash, the library updates the existing Torrent object in place.

from torrentlib import Peer, Torrent

torrent = Torrent(info_hash="1234567890abcdef1234567890abcdef12345678")
peer_id = "-robots-testing12345"
peer_addr = ("127.0.0.1", 6881)

with Peer(peer_addr, torrent, peer_id) as peer:
    # Request every metadata piece advertised by the peer.
    if peer.metadata_size is not None:
        peer.request_all_metadata()
        peer.read_all()
    
    # Inspect the downloaded metadata.
    if torrent.has_metainfo:
        print("Metadata downloaded")
        print(torrent.name)
        print(torrent.total_size)

        files = torrent.get_files_info()
        if files:
            for file_hash, file_info in files.items():
                print(file_hash, file_info["name"], file_info["length"])

Clone this wiki locally