Skip to content

Tracker Queries

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

Introduction

After creating a Torrent object, query a tracker to discover peers participating in the torrent swarm. Tracker responses identify peers using (IP address, port) pairs.

Querying trackers

Tracker queries operate on a Torrent object, not a raw info_hash. The library reads torrent.left, torrent.downloaded, torrent.uploaded, and torrent.event from that object.

Querying one tracker

Query.single() chooses HTTP or UDP based on the tracker URL:

from torrentlib import Torrent, TorrentStatus
from torrentlib.Tracker import Query

torrent = Torrent(
    info_hash="1234567890abcdef1234567890abcdef12345678",
    event=TorrentStatus.STARTED,
)
peer_id = "-robots-testing12345"

response = Query.single(
    torrent=torrent,
    url="udp://tracker.opentrackr.org:1337/announce",
    peer_id=peer_id,
    port=6881,
    timeout=10,
)

print(response["interval"])
print(response.get("seeders"))
print(response.get("leechers"))
print(len(response.get("peers", [])))
print(len(response.get("peers6", [])))

Querying multiple trackers

You can also query multiple trackers concurrently:

from torrentlib.Tracker import Query

responses = Query.multi(
    torrent=torrent,
    urls=[
        "udp://tracker.opentrackr.org:1337/announce",
        "http://tracker.example.com:8080/announce",
    ],
    peer_id=peer_id,
    port=6881,
    timeout=10,
)

for url, result in responses.items():
    if "error" in result:
        print(url, result["error"])
    else:
        print(url, len(result.get("peers", [])))

Successful tracker queries automatically merge returned peers into:

  • torrent.peers for IPv4 peers
  • torrent.peers6 for IPv6 peers

Both query methods accept optional ip_addr, num_want, key, port, and timeout arguments. HTTP queries also accept custom headers. Query.multi() additionally accepts max_threads to limit concurrent requests.

Clone this wiki locally