Python bindings for librqbit - a feature-rich BitTorrent client library
Project description
pyrqbit
Python bindings for librqbit - a feature-rich BitTorrent client library written in Rust.
Installation
Install from PyPI (recommended)
We provide pre-built wheels for the latest versions of Python:
pip install pyrqbit
Build from Source
Prerequisites:
- Python 3.10 or higher
- Rust toolchain (for building from source)
- Git (for cloning with submodules)
# Clone the repository with submodules
git clone --recursive https://github.com/fakerybakery/pyrqbit.git
cd pyrqbit
# Install maturin (Rust-Python build tool)
pip install maturin
# Build and install in development mode
maturin develop --release
# Or build a wheel for distribution
maturin build --release
pip install target/wheels/pyrqbit-*.whl
Quick Start
pyrqbit provides two APIs: a high-level API (recommended) and a low-level API (for advanced use).
High-Level API (Recommended)
Pythonic interface with context managers and progress callbacks:
from pyrqbit import TorrentClient
# Context manager automatically handles cleanup
with TorrentClient("./downloads") as client:
# Download with progress callback
client.download(
"magnet:?xt=urn:btih:...",
progress_callback=lambda p: print(f"{p.progress_percent:.1f}% - {p.download_speed:.2f} MB/s"),
)
Even simpler - one-liner download:
from pyrqbit import quick_download
# Automatically shows progress and cleans up
quick_download("magnet:?xt=urn:btih:...")
Download only specific files:
from pyrqbit import TorrentClient
with TorrentClient("./downloads") as client:
# Download only MP4 files
client.download(
"magnet:?xt=urn:btih:...",
include_pattern=r'\.mp4$',
progress_callback=lambda p: print(p),
)
Low-Level API
Direct bindings to librqbit for fine-grained control:
import pyrqbit
import time
# Create a session
session = pyrqbit.Session(
output_folder="/path/to/downloads",
listen_port=6881,
)
# Add a torrent
torrent = session.add_torrent(
"magnet:?xt=urn:btih:...",
paused=False,
overwrite=True
)
# Monitor progress manually
while True:
stats = torrent.stats()
print(f"Progress: {stats['progress_percent']:.1f}%")
print(f"Speed: {stats['download_speed']:.2f} MB/s")
if stats['progress_percent'] >= 100.0:
break
time.sleep(1)
session.stop()
Selective File Download
import pyrqbit
session = pyrqbit.Session("/path/to/downloads")
# Download only video files using regex
torrent = session.add_torrent(
"magnet:?xt=urn:btih:...",
only_files_regex=r"\.(mp4|mkv|avi)$"
)
# Or download specific files by index
torrent = session.add_torrent(
"/path/to/file.torrent",
only_files=[0, 2, 5] # Download files 0, 2, and 5
)
# List all files in the torrent
for file_info in torrent.list_files():
print(f"{file_info['index']}: {file_info['path']} ({file_info['size']} bytes)")
print(f" Included: {file_info['included']}")
# Update file selection after adding
torrent.update_only_files([1, 3, 4])
Advanced Usage
import pyrqbit
# Create session with custom options
session = pyrqbit.Session(
output_folder="/downloads",
listen_port=6881,
disable_dht=False,
disable_trackers=False
)
# Add torrent from local file
torrent = session.add_torrent(
"/path/to/file.torrent",
output_folder="/custom/output", # Override default
overwrite=True
)
# Pause and resume
torrent.pause()
print(f"Paused: {torrent.is_paused}")
# Wait for completion with timeout
if torrent.wait_until_completed(timeout_secs=3600):
print("Download finished!")
else:
print("Timeout reached")
# Get torrent info
print(f"Torrent ID: {torrent.id}")
print(f"Info Hash: {torrent.info_hash}")
# List all torrents
for torrent_id, info_hash in session.list_torrents():
print(f"{torrent_id}: {info_hash}")
# Delete torrent (keep files)
session.delete_torrent(torrent.id, delete_files=False)
# Stop session
session.stop()
API Reference
Session
class Session:
def __init__(
self,
output_folder: str,
listen_port: Optional[int] = None,
disable_dht: bool = False,
disable_trackers: bool = False,
)
Create a new torrent session.
Methods:
add_torrent(source, output_folder=None, only_files=None, only_files_regex=None, paused=None, overwrite=None) -> TorrentHandleget_torrent(torrent_id: int) -> Optional[TorrentHandle]list_torrents() -> List[Tuple[int, str]]delete_torrent(torrent_id: int, delete_files: bool = False)stop()
TorrentHandle
Properties:
id: int- Torrent IDinfo_hash: str- Info hash as hex stringis_paused: bool- Whether torrent is pausedis_live: bool- Whether torrent is active
Methods:
stats() -> Dict- Get torrent statisticspause()- Pause the torrentlist_files() -> List[Dict]- List files in torrentupdate_only_files(file_indices: List[int])- Update file selectionwait_until_completed(timeout_secs: Optional[int] = None) -> bool- Wait for completion
Supported Torrent Sources
- Magnet links:
magnet:?xt=urn:btih:... - HTTP(S) URLs: Direct links to .torrent files
- Info hashes: 40-character hex strings
- Local files: Path to .torrent files
Examples
See the examples/ directory for more detailed examples:
examples/basic_download.py- Simple torrent downloadexamples/selective_download.py- Download specific filesexamples/monitor_progress.py- Real-time progress monitoring
Building and Development
Setup Development Environment
# Clone with submodules
git clone --recursive https://github.com/fakerybakery/pyrqbit.git
cd pyrqbit
# Create virtual environment
python -m venv venv
source venv/bin/activate # or `venv\Scripts\activate` on Windows
# Install development dependencies
pip install maturin pytest
# Build in development mode
maturin develop
Running Tests
pytest tests/
Building Release
# Build release wheel
maturin build --release
# The wheel will be in target/wheels/
ls target/wheels/
License
Like rqbit, pyrqbit is licensed under the Apache License, Version 2.0. See LICENSE for details.
Credits
This project wraps rqbit by Igor Katson.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
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 Distribution
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 pyrqbit-0.1.0.tar.gz.
File metadata
- Download URL: pyrqbit-0.1.0.tar.gz
- Upload date:
- Size: 798.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7f7b06dee9327b19bf059d571ea1f4ec4e69f998f94e130cceb8c62400084768
|
|
| MD5 |
c3bef83c32494d7ba1281166edc84b8a
|
|
| BLAKE2b-256 |
2445e1006c88a64baa9f70d934b59bc9759aa37d8db400f720fbaa8953555a75
|
File details
Details for the file pyrqbit-0.1.0-cp38-abi3-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: pyrqbit-0.1.0-cp38-abi3-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 5.0 MB
- Tags: CPython 3.8+, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7222da4a9669179c4ec33068d7406099185b43a81f30cd8d3163dcb8c4467d12
|
|
| MD5 |
24588616e24c67f134d3a0a30dd7e304
|
|
| BLAKE2b-256 |
46de0d18b117a9196f28d8b2af2c2a011a24540deaa52b3f6ed9ebce48b5b7a2
|