Skip to main content

Full implementation of the BitTorrent mainline DHT

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pythontorrentdht-1.0.1.tar.gz (68.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pythontorrentdht-1.0.1-py3-none-any.whl (72.5 kB view details)

Uploaded Python 3

File details

Details for the file pythontorrentdht-1.0.1.tar.gz.

File metadata

  • Download URL: pythontorrentdht-1.0.1.tar.gz
  • Upload date:
  • Size: 68.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pythontorrentdht-1.0.1.tar.gz
Algorithm Hash digest
SHA256 51e42df7702c6e85fdc894635b16cbecd23eb383374bc5ed03dd650793e6ee50
MD5 c517e5a27fc506440943fb1f90699a2b
BLAKE2b-256 e1b9de0da235fe86a17857c2d9b8dd4281506cdaf0e9a3f7aabef70508a7458d

See more details on using hashes here.

Provenance

The following attestation bundles were made for pythontorrentdht-1.0.1.tar.gz:

Publisher: publish.yml on Sprooty/pythonTorrentDHT

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pythontorrentdht-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pythontorrentdht-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 702af77131204d02a51044e3ab26dbde08df18dd21a48f25bd6f4d08c15f29a4
MD5 3ae74f9876d6ff310a53760fcfbf5664
BLAKE2b-256 c4543cbfe0690236ae1d9f29dbb0b902aef4ddbb569ee5b73440db2308f04c14

See more details on using hashes here.

Provenance

The following attestation bundles were made for pythontorrentdht-1.0.1-py3-none-any.whl:

Publisher: publish.yml on Sprooty/pythonTorrentDHT

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page