How to Create Download Manager in Python














































How to Create Download Manager in Python



How to Create Download Manager in Python 

This article demonstrate that " How to Create Download Manager in Python" .
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.


Prerequisites :

 -  A computer machine with windows.
 -  Python already installed in system.

Build a Set-up :

We need to download the mentioned packages from command prompt.


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




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



       
        3. Threading package: To work with threads we need threading package.

                                   pip install threading



 The above error is complaining that it can't find this lib, which makes sense. Because this threading is     already in python's  standard library, You don't need to manually install it.

 We can write it from the command line. Try this: python -c 'import' threading . If that doesn't give you an error,   then we're fine.





Implementation :
Note: The program has been split into parts to make it easy to understand.

# Importing the required packages 
import click 
import requests 
import threading
  
# The below code is used for each chunk of file handled by each thread for downloading  
# the content from specified location to storage 
def Handler(startendurlfilename): 
     
    # specify the starting and ending of the file 
    headers = {'Range''bytes=%d-%d' % (start, end)} 
  
    # request the specified part and get into variable     
    r = requests.get(url, headers=headers, stream=True
  
    # open the file and write the content of the html page  
    # into file. 
    with open(filename, "r+b"as f: 
      
        f.seek(start)
        var = f.tell() 
        f.write(r.content)


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.
@click.command(help="It downloads the specified file with specified name"
@click.option('--number_of_threads',default=4help="No of Threads"
@click.option('--name',type=click.Path(),help="Name of the file with extension"
@click.argument('url_of_file',type=click.Path()) 
@click.pass_context 
def download_file(ctx,url_of_file,name,number_of_threads): 
   r = requests.head(url_of_file) 
   if name: 
     file_name = name
   else
     file_name = url_of_file.split('/')[-1
   try
     file_size = int(r.headers['content-length']) 
   except
    print ("Invalid URL")
    return


  • In this above "download_file" 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.
Now ,create a file with size of the content :-
part = int(file_size) / number_of_threads 
   fp = open(file_name, "w"
   fp.write('%uFFFD' * file_size) 
   fp.close()


Now we create Threads and pass the Handler function which has the main functionality :-
for i in range(number_of_threads): 
     start = part * i 
     end = start + part 
  
          # create a Thread with start and end locations 
     t = threading.Thread(target=Handler, kwargs={'start': start, 'end': end, 'url': url_of_file, 'filename': file_name})
     t.setDaemon(True
     t.start()

Finally join the threads and call the download_file function from main :-
main_thread = threading.current_thread() 
for t in threading.enumerate(): 
     if t is main_thread: 
        continue
     t.join() 
print(" %s downloaded " % file_name )
  
if __name__ == '__main__'
       download_file(obj={})



So the coding part is complete, now we can run our .py file with the following command :

python file_name.py --help


This command shows the Usage of the click command tool and options that the tool can accepts.



General Command to download anything :

python  file_name.py  --number_of_threads  --name=file_name  url_of_file




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





Now I can see the downloaded image in my cppsecrets folder in my pc :

        
  
   
So finally, a Download Manager is created which is basically a computer program dedicated to the task of downloading stand alone files from internet. Now we can download any file from this download manager via the target download file url .


   *****END OF ARTICLE******



More Articles of Vishal Lodhi:

Name Views Likes
Python string zfill 137 0
Python string swapcase 108 0
Python string title 109 0
Python string startswith 118 0
Python string replace 159 0
Python string translate 99 0
Python string rpartition 96 0
Python string partition 92 0
Python string splitlines 138 0
Python string rsplit 90 0
Python string split 90 0
Python string rindex 99 0
Python string rfind 121 0
Python string upper 95 0
Python string lower 97 0
Python string maketrans 104 0
Python string strip 95 0
Python string rstrip 115 0
Python string lstrip 96 0
Python string rjust 114 0
Python string ljust 113 0
Python string len 97 0
Python string join 92 0
Python string casefold 97 0
Python string isprintable 102 0
Python string encode 95 0
Python string isdecimal 101 0
Python string isidentifier 93 0
Python string isupper 96 0
Python string istitle 94 0
Python string isspace 115 0
Python string isnumeric 118 0
Python string isdigit 99 0
Python string islower 100 0
Python string isalpha 115 0
Python string isalnum 114 0
Python string index 103 0
Python string find 109 0
Python string expandtabs() 91 0
Python string endswith 109 0
Python string count 105 0
Python string capitalize 92 0
Python string center 96 0
Python string Introduction 115 0
Python string Template 160 0
Python string Formatter 169 0
Python string printable 246 0
Python string whitespace 163 0
Python string punctuation 119 0
Python string octdigits 104 0
Python string hexdigits 125 0
Python string digits 91 0
Python string ascii_letters 426 0
Python string ascii_lowercase 204 0
Python string ascii_uppercase 198 0
How to Create Download Manager in Python 772 0
Python random weibullvariate 185 0
python random paretovariate 151 0
Python random vonmisesvariate 198 0
Python random normalvariate 228 0
Python random lognormvariate 134 0
Python random gauss 142 0
Python random gammavariate 158 0
Python random expovariate 232 0
Python random betavariate 235 0
Python random triangular 128 0
Python random uniform 165 0
Python random random 129 0
Python random sample 213 0
Python random shuffle 198 0
Python random choices 278 0
Python random choice 180 0
Python random randint 165 0
Python random randrange 259 0
Python random getrandbits 167 0
Python random setstate 162 0
Python random getstate 207 0
Python random seed 185 1
Python random Introduction 193 0

Comments