Skip to main content

Pure Python asyncio BitTorrent client library

Project description

aiobt

Pure Python asyncio BitTorrent client library.

Features

  • Fully async — built on asyncio from the ground up with an async context manager interface
  • Zero dependencies — pure stdlib, no runtime dependencies
  • Pluggable storage — swap the filesystem backend for S3, databases, or anything else
  • Compact mode — store multi-file torrents as a single blob for distribution services
  • LAN peer discovery — BEP 26 Local Service Discovery via multicast, no tracker needed on local networks
  • Cython-ready — hot paths like bencode are structured for optional Cython compilation
  • Modern Python — requires 3.14+, uses type aliases, match, TaskGroup, frozen dataclasses
  • Type-safe — fully typed with py.typed marker, checked with pyrefly

Installation

pip install aiobt

Quick Start

import asyncio
from aiobt import Client
from aiobt.storage import DiskStorage

async def main() -> None:
    storage = DiskStorage("/tmp/downloads")

    async with Client(storage=storage) as client:
        torrent = await client.add_torrent_file("archlinux-2026.05.01-x86_64.iso.torrent")
        print(f"Downloading: {torrent.info.name}")
        print(f"Size: {torrent.total_length} bytes")
        print(f"Pieces: {torrent.piece_count}")

        await client.download(torrent.info_hash)

asyncio.run(main())

Compact Storage for Distribution

For seeding servers or CDN nodes where you want simple file management, use CompactStorage to store even multi-file torrents as a single blob:

from aiobt import Client
from aiobt.storage import CompactStorage

async with Client(storage=CompactStorage("/srv/torrents")) as client:
    # Multi-file torrent stored as one file on disk
    torrent = await client.add_torrent_file("linux-distro.torrent")
    await client.seed(torrent.info_hash)

Custom Storage Backends

Implement the StorageBackend protocol to plug in any storage:

from aiobt.storage import StorageBackend

class S3Storage:
    """Store torrent data in S3."""

    async def open(self, total_length: int, piece_length: int) -> None:
        self._bucket = await create_bucket()

    async def read(self, offset: int, length: int) -> bytes:
        return await self._bucket.get_range(offset, length)

    async def write(self, offset: int, data: bytes) -> None:
        await self._bucket.put_range(offset, data)

    async def close(self) -> None:
        await self._bucket.close()

Local Peer Discovery (BEP 26)

Find peers on the local network without a tracker — ideal for LAN parties, office setups, or air-gapped environments:

import asyncio
from aiobt import LocalDiscovery

async def main() -> None:
    async with LocalDiscovery(listen_port=6881) as lsd:
        # Announce a torrent we're serving
        lsd.announce(info_hash)

        # Discover peers on the LAN
        async for peer in lsd.discovered_peers():
            print(f"LAN peer: {peer.host}:{peer.port}")
            # connect and exchange pieces...

asyncio.run(main())

LocalDiscovery uses IPv4 multicast (239.192.152.143:6771) with optional IPv6 support. It automatically filters out its own announcements via a per-instance cookie.

Architecture

aiobt/
├── client.py       # Client async context manager
├── bencode.py      # Bencode codec (Cython-ready)
├── torrent.py      # Torrent metadata — frozen dataclass models
├── discovery.py    # Local Service Discovery — BEP 26 multicast
├── peer.py         # Peer connection management
├── tracker.py      # HTTP + UDP tracker announce
├── protocol.py     # BitTorrent wire protocol (BEP 3)
├── piece.py        # Piece selection, verification, assembly
└── storage/
    ├── base.py     # StorageBackend protocol
    ├── disk.py     # Standard multi-file on-disk storage
    ├── compact.py  # Single-blob storage for distribution
    └── queue.py    # Executor-backed filesystem I/O queue

Development

git clone https://github.com/fried/aiobt.git
cd aiobt
pip install -e ".[dev]"

# Run tests
python -m unittest discover tests

# Format
black src/ tests/

# Type check
pyrefly check src/

Optional Cython Compilation

The bencode module ships with a Cython-optimized variant (bencode.pyx) that compiles automatically when building from source via the Meson backend:

pip install meson-python meson ninja cython
pip install -e ".[dev]" --no-build-isolation

Verify it loaded:

from aiobt import is_compiled, compilation_status
print(compilation_status())  # {'bencode': True}

Both .so and .py are always shipped — Python prefers the compiled extension but falls back to pure Python automatically.

See cython/README.md for details.

CI / CD

  • CI runs on every push and PR: black formatting, pyrefly type checking, pure Python tests, and Cython compile + test.
  • Release on v* tags: builds sdist and platform wheels (Linux, macOS arm64/x86_64, Windows) with Cython, tests them, then publishes to PyPI via trusted publishing.

License

MIT

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

aiobt-0.1.0a1.tar.gz (37.9 kB view details)

Uploaded Source

Built Distributions

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

aiobt-0.1.0a1-cp314-cp314-win_amd64.whl (153.9 kB view details)

Uploaded CPython 3.14Windows x86-64

aiobt-0.1.0a1-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (144.6 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

aiobt-0.1.0a1-cp314-cp314-macosx_14_0_arm64.whl (109.2 kB view details)

Uploaded CPython 3.14macOS 14.0+ ARM64

File details

Details for the file aiobt-0.1.0a1.tar.gz.

File metadata

  • Download URL: aiobt-0.1.0a1.tar.gz
  • Upload date:
  • Size: 37.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for aiobt-0.1.0a1.tar.gz
Algorithm Hash digest
SHA256 f60d443f9082565292d6a5eeaf9f636891e6c1be03e4b7de61bf6373f2675e84
MD5 aaf02dbf0ecc73cdbfac2bf4cd933622
BLAKE2b-256 7704fb60b2b3a35d9d610dfcf30d859c056f2a5c7892d0e0887f18066e971df8

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiobt-0.1.0a1.tar.gz:

Publisher: release.yml on fried/aiobt

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

File details

Details for the file aiobt-0.1.0a1-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: aiobt-0.1.0a1-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 153.9 kB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for aiobt-0.1.0a1-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 0aa531309685f8ac043034516587556bdd5387614fc6c0fff2116cad44b9efb3
MD5 89056f6c6a88a588c3a97a8ab623e7a5
BLAKE2b-256 5cb7137f73d8387425c2dd26cbd0ac5089d9c41c9a89f510b65837655c08bf6f

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiobt-0.1.0a1-cp314-cp314-win_amd64.whl:

Publisher: release.yml on fried/aiobt

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

File details

Details for the file aiobt-0.1.0a1-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for aiobt-0.1.0a1-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 91311c15883e453c5f4a7a3fe26551dc6b6997887f1e4bd842709c43687b936d
MD5 9ca83ebd7ad6718de664d03d223cfdba
BLAKE2b-256 8c6faa7805ee78bd88d09471a1d06b68deccf45ac89757d6c32bd52c2450815e

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiobt-0.1.0a1-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl:

Publisher: release.yml on fried/aiobt

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

File details

Details for the file aiobt-0.1.0a1-cp314-cp314-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for aiobt-0.1.0a1-cp314-cp314-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 6dd5efc0efba20375a4200b22cab7946cf45e89300aa376525101c7dcda9b010
MD5 314383bb32dfd7798d114e358756e593
BLAKE2b-256 2dd14e9d21a3ae453c89ad4dd13b1e4d2cc5be849170ebb3b907a7046851462b

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiobt-0.1.0a1-cp314-cp314-macosx_14_0_arm64.whl:

Publisher: release.yml on fried/aiobt

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