Struggling with multiple programming languages? No worries. Our Code Converter has got you covered. Give it a go!
Have you ever wanted to download files in torrent programmatically? Well, in this tutorial, you will learn how you can download files in torrent using Python.
We will be using qBittorrent here, that's because there is a cool Python wrapper for it that eases everything for us.
To get started, you need to download and install qBittorent official client for your operating system and then install the Python wrapper module using the following command:
pip3 install python-qbittorrent
Now before we dive into the code, we need some configurations to set, after you install the qBittorent client, you need to enable the qBittorrent Web UI using the following steps:
The following image should make everything clear:
Now that we have enabled the Web UI, you can go to the browser and see the qBittorrent web version using the address "127.0.0.1:8080". You'll see a small login page as follows:
Put the credentials you set in the configuration, and then log in, now you should be ready to see the qBittorrent Web UI:
If you're here, then congratulations! You are now ready to use Python to download torrent files, open up a new Python file (or Interactive Python shell), and import the qBittorrent module:
from qbittorrent import Client
Now let's connect and login to the web UI:
# connect to the qbittorent Web UI
qb = Client("http://127.0.0.1:8080/")
# put the credentials (as you configured)
qb.login("admin", "adminadmin")
I have chosen this torrent file for this tutorial, please feel free to use any torrent file you wish (just put it in your current working directory and change the name):
# open the torrent file of the file you wanna download
torrent_file = open("debian-10.2.0-amd64-netinst.iso.torrent", "rb")
Note: If you're not sure what the open() function is doing, check this tutorial.
Let's start downloading:
# start downloading
qb.download_from_file(torrent_file)
If you're executing this cell by cell in an Interactive window, you'll immediately see that a new torrent file appears in both web UI and qBittorrent desktop client as the following figure shows:
Awesome, you can use the savepath parameter to save the resulting file to the path you actually want:
# you can specify the save path for downloads
qb.download_from_file(torrent_file, savepath="/the/path/you/want/to/save")
You can also use the download_from_link() method which takes the magnet URL you want to download:
# this magnet is not valid, replace with yours
magnet_link = "magnet:?xt=urn:btih:e334ab9ddd91c10938a7....."
qb.download_from_link(magnet_link)
You can also do various things, for instance, let's pause all torrents in the client:
# pause all downloads
qb.pause_all()
Or you can resume them:
# resume them
qb.resume_all()
Or even listing them and showing some useful information:
def get_size_format(b, factor=1024, suffix="B"):
"""
Scale bytes to its proper byte format
e.g:
1253656 => '1.20MB'
1253656678 => '1.17GB'
"""
for unit in ["", "K", "M", "G", "T", "P", "E", "Z"]:
if b < factor:
return f"{b:.2f}{unit}{suffix}"
b /= factor
return f"{b:.2f}Y{suffix}"
# return list of torrents
torrents = qb.torrents()
for torrent in torrents:
print("Torrent name:", torrent["name"])
print("hash:", torrent["hash"])
print("Seeds:", torrent["num_seeds"])
print("File size:", get_size_format(torrent["total_size"]))
print("Download speed:", get_size_format(torrent["dlspeed"]) + "/s")
Here is my output:
Torrent name: debian-10.2.0-amd64-netinst.iso
hash: 86d4c80024a469be4c50bc5a102cf71780310074
Seeds: 70
File size: 335.00MB
Download speed: 606.15KB/s
You can also pause and resume specific torrent files using their hash value, this wrapper is rich with useful methods, please check their full API method documentation and the GitHub repository.
Alright, that's it for this tutorial. This will make you open to many cool challenges, here is an example challenge:
By the way, if you wish to use a Python wrapper for the uTorrent client instead, this repository may help.
Read also: How to Transfer Files in the Network using Sockets in Python.
Happy Coding ♥
Just finished the article? Why not take your Python skills a notch higher with our Python Code Assistant? Check it out!
View Full Code Explain My Code
Got a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!