Skip to main content

Aiotorrent

Aiotorrent is an asynchronous, ultra-lightweight torrent library written in pure Python with support for unique features such as serving of files over HTTP without downloading to disk & more.
A comprehensive list of features supported by aiotorrent as of now are:
  • Complete asyncio support
  • Connecting to various types of torrent trackers (HTTP & UDP)
  • Specifying an appropriate piece download strategy (default and sequential downloading)
  • Downloading files to disk
  • Serving files over HTTP without saving them to disk


Requirements

  1. Dependencies: aiotorrent is built to be extremely lite-weight, only 3 dependencies required for the core functionality:
  1. Streaming dependencies: built-in support for streaming files over HTTP. To use this feature, you need to install extra streaming dependencies.

Installation

Install aiotorrent from PyPI using:

$ pip install aiotorrent

In order to use the streaming feature, you can install the dependencies using:

$ pip install aiotorrent[stream-support]

Quickstart

Initialisation

# Import the Torrent class and pass a torrent file to it:
# You can either pass the file path or a file-like object. aiotorrent will handle it internally.
from aiotorrent import Torrent
torrent = Torrent('path/to/file.torrent')

# To initialise the torrent, you need to call the init() coroutine.
await torrent.init()

Getting peers via DHT

While it is not a requirement, it is highly recommended to do use DHT (Distributed Hash Table) for peer discovery, which in turn can lead to better file retrieval speeds. To let aiotorrent perform peer discovery via DHT, you can pass the dht_enabled=True parameter to the init() coroutine, as follows:

await torrent.init(dht_enabled=True)

In some cases this can prevent the torrent from initializing when there are not enough peers available. This behavior will be fixed in a future release.

Downloading & Streaming

Torrent files are stored inside Torrent.files as a list. We can access them by sub-scripting the Torrent.files attribute, like as follows:

Example to download and stream files

# To download the third file
await torrent.download(torrent.files[2])

Similarly, to serve a torrent file over HTTP, you can call the stream() coroutine, as follows:

# To stream the second file, etc
await torrent.stream(torrent.files[1])

This starts up a uvicorn server on localhost:8080 which uses starlette behind the scenes as the ASGI framework to stream files over http.

Piece downloading strategy

You can also change how pieces of a particular file are being downloaded. Currently, this library offers two download strategies:

  • Default: This strategy is used when the strategy parameter is not specified to the Torrent.download() function. It downloads file pieces in a pseudo-sequential manner. For instance, it will start fetching pieces in order, but it may or may not yield the pieces in order.

  • Sequential: This strategy is straight forward, and it always yields pieces in a sequential manner.

from aiotorrent import DownloadStrategy
from aiotorrent import Torrent

torrent = Torrent("path/to/file.torrent")
file = torrent.files[1]

# The download strategies available are: DownloadStrategy.DEFAULT and DownloadStrategy.SEQUENTIAL
await torrent.download(file, strategy=DownloadStrategy.SEQUENTIAL)

Getting the download progress

Each torrent file inside Torrent.files has the following functions to get useful statistics about the download progress:

  1. get_bytes_downloaded(): Returns the total number of bytes downloaded so far for a particular file. This also includes the number of bytes which may not be written to disk (ie the piece validation failed, and the piece was discarded).

  2. get_bytes_written(): Returns the number of bytes written to disk for a particular file.

  3. get_download_progress(precision=2): Returns the download progress in percentage for a particular file. The precision parameter specifies the number of decimal places to round off the result to

file = torrent.files[0]

print(f"Total bytes downloaded: {file.get_bytes_downloaded()} bytes")
# Output: Total bytes downloaded: 1048576 bytes

print(f"Download progress: {file.get_bytes_written()} / {file.size} bytes")
# Output: Download progress: 1048576 / 5242880 bytes

print(f"Download progress: {file.get_download_progress(precision=3)}%")
# Output: Download progress: 20.152%

aiotorrent CLI

Starting from versions 0.9.0, aiotorrent ships with a cli which can be directly invoked from the terminal, if PYTHONPATH is set correctly. The usage is fairly simple, and self explanatory:

$  aiotorrent
usage: aiotorrent [-h] [-v] {download,stream,info} ...

aiotorrent CLI for downloading and streaming torrents.

positional arguments:
  {download,stream,info}
                        Available commands
    download            Download torrent files
    stream              Stream files over HTTP
    info                Parse and show torrent metadata

options:
  -h, --help            show this help message and exit
  -v, --verbose         Show verbose output. Use -vv, -vvv, etc for increased verbosity

Contribution Guidelines

This project is open to contributions. Feel free to open issues on bug reports or feature requests.

Please ensure that you open an issue before submitting a pull request. Also refrain from making pull requests directly to the main branch.


The above methods can take additional parameters to customize the behaviour of this library. Read the documentation to know more.

Metadata

Release files for aiotorrent 0.9.2

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

Source distribution (sdist)

Source distribution for aiotorrent 0.9.2
File Size Uploaded Provenance Actions
aiotorrent-0.9.2.tar.gz 24.5 kB With provenance Details for aiotorrent-0.9.2.tar.gz

Built distribution (wheel)

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

Total release size: 52.6 kB

Release files / aiotorrent-0.9.2.tar.gz

Download URL aiotorrent-0.9.2.tar.gz
Size 24.5 kB
Tags Source
SHA-256 checksum
How to use checksums
6d364a12b315b28810579ae0d78b743496c22927dc6ca9d16b00b043d38f5e43
BLAKE2b-256 checksum
How to use checksums
f5487962734595ec1fa6f07e8c1a65ff57d32e9a64631155c66a6dfa3d785ccf
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 Sep 21, 2025.

Transparency log

Release files / aiotorrent-0.9.2-py3-none-any.whl

Download URL aiotorrent-0.9.2-py3-none-any.whl
Size 28.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b41f25244630e526d48a06a474951a16282941ba59c53cccb1711d118b412139
BLAKE2b-256 checksum
How to use checksums
b25eabc529521dcb455b3639b0f5d7d40838ff99496b6380aee32102bd673e1a
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 Sep 21, 2025.

Transparency log

Security for aiotorrent 0.9.2

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.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for aiotorrent-0.9.2.tar.gz

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 Sep 21, 2025.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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