A Download Manager is basically a computer program dedicated to the task of downloading stand alone files from internet. Here, we are going to create a simple Download Manager with the help of threads in Python. Using multi-threading a file can be downloaded in the form of chunks simultaneously from different threads. To implement this, we are going to create simple command line tool which accepts the URL of the file and then downloads it.
Windows machine with Python installed.
Download the below mentioned packages from command prompt.
- Click package: Click is a Python package for creating beautiful command line interfaces with as little code as necessary. It’s the “Command Line Interface Creation Kit”.
pip install click
- Requests package: In this tool, we are going to download a file based on the URL(HTTP addresses). Requests is an HTTP Library written in Python which allows you to send HTTP requests. You can add headers, form data, multi-part files, and parameters with simple Python dictionaries and access the response data in the same way.
pip install requests
- threading package: To work with threads we need threading package.
pip install threading
(Note: The program has been split into parts to make it easy to understand. Make sure that you are not missing any part of the code while running the code.)
- Create new python file in editor
- First import the required packages to that you’ll need to write
Now we are going to implement the actual functionality of in the download_file function.
- The first step is to decorate the function with click.command() so that we can add command line arguments. We can also give options for respective commands.
- For our implementation upon entering command –help we are going to display the options that can be used. In our program there are two options that can be used. One is “number_of_threads” and the other is “name”. By default “number_of_threads” is taken as 4. To change it, we can specify while running the program.
- “name” option is given so that we can give our own name to the file that is going to be downloaded. The arguments for the function can be specified using click.argument().
- For our program we need to give the URL of the file we want to download.
The following code goes under the “download_file” function.
- In this function we first check for the “name”. If the “name” is not given then use the name from url.
- Next step is to connect to the URL and Get the content and size of the content.
Create file with size of the content
Now we create Threads and pass the Handler function which has the main functionality :
Finally join the threads and call the “download_file” function from main
We are done with coding part and now follow the commands showed below to run the .py file.
“python filename.py” –-help
This command shows the “Usage” of the click command tool and options that the tool can accepts.
Below is the sample command where we try to download an jpg image file from a URL and also gave a name and number_of_threads.
Finally, we are successfully done with it and this is one of the way to build a simple multithreaded download manager in Python.
This article is contributed by Rahul Bojanapally. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Context Manager in Python
- How to download Google Images using Python
- Python | Download YouTube videos using youtube_dl module
- Python | Program to download complete Youtube playlist
- Pytube | Python library to download youtube videos
- YouTube Media/Audio Download using Python | pafy
- Python | How to download windows lock-screen wallpapers
- Python | Simple calculator using Tkinter
- Python | Simple GUI calculator using Tkinter
- Simple Chat Room using Python
- Simple Keyboard Racing with Python
- Python | Make a simple window using kivy
- Python | Simple FLAMES game using Tkinter
- Python | Simple registration form using Tkinter
- Python | Program to implement simple FLAMES game