How to Download Torrent Files in Python

Using Python wrapper for qBittorrent Web API to automatically download, pause and handle torrent files in Python.
  · 5 min read · Updated may 2024 · Application Programming Interfaces

Ready to take Python coding to a new level? Explore our Python Code Generator. The perfect tool to get your code up and running in no time. Start now!

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:

  • Once you have everything set, launch qBittorrent. On the menu bar, go to Tools > Options qBittorrent WEB UI.
  • When the new window appears, choose the Web UI option.
  • Check the "Web User Interface (Remote Control)" checkbox.
  • You can choose a port (the default is 8080).
  • Set username and password (the default is admin:adminadmin).

The following image should make everything clear:

Enabling qBittorrent Web UINow that we have enabled the Web UI, you can go to the browser and see the qBittorrent web version using the address "". You'll see a small login page as follows:

qBittorrent Web UI login prompt

Put the credentials you set in the configuration, and then log in, now you should be ready to see the qBittorrent Web UI:

Web UI version of qBittorrentIf 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("")

# 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

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:

Start downloading the torrent file

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....."

You can also do various things, for instance, let's pause all torrents in the client:

# pause all downloads

Or you can resume them:

# resume them

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
        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 ♥

Take the stress out of learning Python. Meet our Python Code Assistant – your new coding buddy. Give it a whirl!

View Full Code Fix My Code
Sharing is caring!

Read Also

Comment panel

    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!