Skip to content

Metadata Download

Jacky He edited this page Jun 14, 2026 · 2 revisions

After connecting to a peer and if you start with only an info hash, you can fetch metadata from a peer that supports ut_metadata. Once the full metadata is assembled and verified, 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:
    # retrieve metadata from peer
    if peer.metadata_size is not None:
        peer.request_all_metadata()
        peer.read_all()
    
    # print out the metadata downloaded
    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