1

I'm trying to quickly download kali-linux live-amd64.iso.torrent file from kali.org/downloads using requests, and bs4, but in the headers of the torrent response, there is no: response.headers['content-disposition'] My reference was from: How to download a file with .torrent extension from link with Python. I looked into the headers of the torrent_response, and this is what came up:

{'Server': 'nginx/1.14.2', 'Date': 'Thu, 08 Apr 2021 07:46:17 GMT', 'Content-Type': 'application/octet-stream', 'Content-Length': '274612', 'Connection': 'keep-alive', 'Last-Modified': 'Wed, 24 Feb 2021 17:39:18 GMT', 'ETag': '"60368f46-430b4"', 'X-Cache-Status': 'HIT', 'Accept-Ranges': 'bytes'}

class Download_Kali:
    
    def __init__(self, locator):
        self.locator = locator

    def locate_torrent(self):
        torrent_links = [torrent['href'] for torrent in self.locator.findAll('a', string='Torrent')]
        for live_iso in torrent_links:
            if 'live-amd64' in live_iso:
                print('[*] Downloading iso Torrent')
                return live_iso

    def install(self):
        import re, traceback
        try:
            with requests.get(Download_Kali(self.locator).locate_torrent()) as torrent_response:
                torrent_response.raise_for_status()
                
                disposition = torrent_response.headers
                print(disposition)
                #torrent_file = re.findall('filename="(.+)"', disposition)
                #if torrent_file:
                #    with open(torrent_file[0], 'wb') as f_torrent:
                #        f_torrent.write(torrent_response.content)
        except requests.HTTPError as err:
            print(traceback.format_exc())
Matt Millar
  • 102
  • 1
  • 8

1 Answers1

2

Here's my take on this if you want to go for .iso image. I'm using the Net Installer as it's lighter in size. Also, I've added a progress bar.

import shutil

import requests
from bs4 import BeautifulSoup
from tqdm import tqdm

with requests.Session() as connection:
    kali_page = connection.get("https://kali.org/downloads/").content
    iso_images = [
        t["href"] for t in
        BeautifulSoup(kali_page, "lxml").select("table a")
        if not t["href"].endswith(".torrent")
    ]
    for iso_image in iso_images:
        if "netinst-arm64" in iso_image:
            print(f"Downloading {iso_image}")
            file_name = iso_image.rsplit("/")[-1]
            with connection.get(iso_image, stream=True) as response, \
                    open(file_name, "wb") as output:
                total_size = int(response.headers.get('content-length', 0))
                with tqdm(
                        total=total_size / (32 * 1024.0),
                        unit='B',
                        unit_scale=True,
                        unit_divisor=1024,
                ) as progress_bar:
                    for data in response.iter_content(32 * 1024):
                        progress_bar.update(len(data))
                shutil.copyfileobj(response.raw, output)

This should show something like this:

enter image description here

baduker
  • 19,152
  • 9
  • 33
  • 56