Pure Python asyncio BitTorrent client library
Project description
aiobt
Pure Python asyncio BitTorrent client library.
Features
- Fully async — built on
asynciofrom 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
typealiases,match,TaskGroup, frozen dataclasses - Type-safe — fully typed with
py.typedmarker, 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f60d443f9082565292d6a5eeaf9f636891e6c1be03e4b7de61bf6373f2675e84
|
|
| MD5 |
aaf02dbf0ecc73cdbfac2bf4cd933622
|
|
| BLAKE2b-256 |
7704fb60b2b3a35d9d610dfcf30d859c056f2a5c7892d0e0887f18066e971df8
|
Provenance
The following attestation bundles were made for aiobt-0.1.0a1.tar.gz:
Publisher:
release.yml on fried/aiobt
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aiobt-0.1.0a1.tar.gz -
Subject digest:
f60d443f9082565292d6a5eeaf9f636891e6c1be03e4b7de61bf6373f2675e84 - Sigstore transparency entry: 1689294375
- Sigstore integration time:
-
Permalink:
fried/aiobt@0737de09d9989d20c221e2fefb31f66d064c2d22 -
Branch / Tag:
refs/tags/v0.1.0a1 - Owner: https://github.com/fried
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@0737de09d9989d20c221e2fefb31f66d064c2d22 -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0aa531309685f8ac043034516587556bdd5387614fc6c0fff2116cad44b9efb3
|
|
| MD5 |
89056f6c6a88a588c3a97a8ab623e7a5
|
|
| BLAKE2b-256 |
5cb7137f73d8387425c2dd26cbd0ac5089d9c41c9a89f510b65837655c08bf6f
|
Provenance
The following attestation bundles were made for aiobt-0.1.0a1-cp314-cp314-win_amd64.whl:
Publisher:
release.yml on fried/aiobt
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aiobt-0.1.0a1-cp314-cp314-win_amd64.whl -
Subject digest:
0aa531309685f8ac043034516587556bdd5387614fc6c0fff2116cad44b9efb3 - Sigstore transparency entry: 1689294446
- Sigstore integration time:
-
Permalink:
fried/aiobt@0737de09d9989d20c221e2fefb31f66d064c2d22 -
Branch / Tag:
refs/tags/v0.1.0a1 - Owner: https://github.com/fried
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@0737de09d9989d20c221e2fefb31f66d064c2d22 -
Trigger Event:
push
-
Statement type:
File details
Details for the file aiobt-0.1.0a1-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.
File metadata
- Download URL: aiobt-0.1.0a1-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
- Upload date:
- Size: 144.6 kB
- Tags: CPython 3.14, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
91311c15883e453c5f4a7a3fe26551dc6b6997887f1e4bd842709c43687b936d
|
|
| MD5 |
9ca83ebd7ad6718de664d03d223cfdba
|
|
| BLAKE2b-256 |
8c6faa7805ee78bd88d09471a1d06b68deccf45ac89757d6c32bd52c2450815e
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aiobt-0.1.0a1-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl -
Subject digest:
91311c15883e453c5f4a7a3fe26551dc6b6997887f1e4bd842709c43687b936d - Sigstore transparency entry: 1689294422
- Sigstore integration time:
-
Permalink:
fried/aiobt@0737de09d9989d20c221e2fefb31f66d064c2d22 -
Branch / Tag:
refs/tags/v0.1.0a1 - Owner: https://github.com/fried
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@0737de09d9989d20c221e2fefb31f66d064c2d22 -
Trigger Event:
push
-
Statement type:
File details
Details for the file aiobt-0.1.0a1-cp314-cp314-macosx_14_0_arm64.whl.
File metadata
- Download URL: aiobt-0.1.0a1-cp314-cp314-macosx_14_0_arm64.whl
- Upload date:
- Size: 109.2 kB
- Tags: CPython 3.14, macOS 14.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6dd5efc0efba20375a4200b22cab7946cf45e89300aa376525101c7dcda9b010
|
|
| MD5 |
314383bb32dfd7798d114e358756e593
|
|
| BLAKE2b-256 |
2dd14e9d21a3ae453c89ad4dd13b1e4d2cc5be849170ebb3b907a7046851462b
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aiobt-0.1.0a1-cp314-cp314-macosx_14_0_arm64.whl -
Subject digest:
6dd5efc0efba20375a4200b22cab7946cf45e89300aa376525101c7dcda9b010 - Sigstore transparency entry: 1689294472
- Sigstore integration time:
-
Permalink:
fried/aiobt@0737de09d9989d20c221e2fefb31f66d064c2d22 -
Branch / Tag:
refs/tags/v0.1.0a1 - Owner: https://github.com/fried
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@0737de09d9989d20c221e2fefb31f66d064c2d22 -
Trigger Event:
push
-
Statement type: