Skip to main content

pythonTorrentDHT

A Python implementation of the BitTorrent Distributed Hash Table, extending nitmir/btdht with additional BEP support.

License: GPL v3 Python 3.10+

Overview

pythonTorrentDHT provides a full Python implementation of the BitTorrent mainline DHT. It is a natural evolution of nitmir/btdht, modernised for Python 3.10+ (pure Python, no Cython) and extended with additional BitTorrent Enhancement Proposals:

BEP Description
BEP 5 BitTorrent DHT Protocol — core routing table, get_peers, announce_peer
BEP 9 Extension for Peers to Send Metadata Files — fetch .torrent info-dicts without a tracker
BEP 10 Extension Protocol — peer capability negotiation (required by BEP 9)
BEP 51 DHT Infohash Indexing — sample_infohashes for crawling the DHT

Requirements

No C compiler required.

Installation

pip install pythontorrentdht

Or from source:

git clone https://github.com/Sprooty/pythonTorrentDHT
cd pythonTorrentDHT
pip install -e .

Usage

Find peers for a torrent

import btpydht
import binascii

dht = btpydht.DHT()
dht.start()  # allow ~15s to bootstrap

peers = dht.get_peers(binascii.a2b_hex("0403fb4728bd788fbcb67e87d6feb241ef38c75a"))
print(peers)
# [('81.171.107.75', 17744), ('94.242.250.86', 3813), ...]

dht.stop()

Fetch torrent metadata (BEP 9/10)

Retrieve the info-dict directly from a peer without a .torrent file:

from btpydht.metadata import get_metadata

info_hash = binascii.a2b_hex("0403fb4728bd788fbcb67e87d6feb241ef38c75a")
peers = dht.get_peers(info_hash)

metadata = get_metadata(info_hash, peers[0])
print(metadata[b"name"])

Sample infohashes from the DHT (BEP 51)

Walk the DHT and collect infohashes being announced:

class CrawlerDHT(btpydht.DHT_BASE):
    def on_sample_infohashes_response(self, response):
        for ih in response.get(b"samples", []):
            print(ih.hex())

dht = CrawlerDHT()
dht.start()

Announce a torrent

info_hash = binascii.a2b_hex("0403fb4728bd788fbcb67e87d6feb241ef38c75a")
dht.announce_peer(info_hash, port=6881)

Extend with custom message handlers

Subclass btpydht.DHT_BASE and override on_<msg>_query / on_<msg>_response:

class MyDHT(btpydht.DHT_BASE):
    def on_get_peers_query(self, query):
        print(f"Peer request for: {query[b'info_hash'].hex()}")

dht = MyDHT()
dht.register_message(b"get_peers")
dht.start()

Save and restore routing table state

dht.save("routing_table.dat")
# later...
dht.load("routing_table.dat")

Development

pip install -r requirements-dev.txt
make test

Or run pytest directly:

python -m pytest tests/ -v

License

GPLv3 — see LICENSE for details.

Credits

Originally created by Valentin Samir.

Metadata

Release files for pythontorrentdht 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pythontorrentdht 1.0.3
File Size Uploaded
pythontorrentdht-1.0.3.tar.gz 70.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pythontorrentdht 1.0.3
File Interpreter ABI Platform
pythontorrentdht-1.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 145.3 kB

Release files / pythontorrentdht-1.0.3.tar.gz

Download URL pythontorrentdht-1.0.3.tar.gz
Size 70.8 kB
Tags Source
SHA-256 checksum
How to use checksums
6cbc4a3ce75a402cc40c14d539b18e90401cca039311cddd99f31480b3b33a05
BLAKE2b-256 checksum
How to use checksums
bbaaa92a3d3673624f6ce4befcdcf446cc995c007be3d2e950cd14a882c8829c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 16, 2026.

Transparency log

Release files / pythontorrentdht-1.0.3-py3-none-any.whl

Download URL pythontorrentdht-1.0.3-py3-none-any.whl
Size 74.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b622f42e3c6ca0a33a55e449cb96031efd83f20e1f6e25bb6da69400bdcad57a
BLAKE2b-256 checksum
How to use checksums
c871f0f68e259b949ff8775117eebc06be8cdc453a3e62274770abb30dcac25c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 16, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.3 This release

2 release files

1.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page