Please help me to fix errors in Python code below and make it slightly beautiful.

This Python code is a proxy speed tester. It takes a list of proxies and a list of URLs as input, and then uses multiple threads to download a random URL from each proxy, measuring the download speed in MBps. The results are sorted by speed, with the fastest proxies first.

The problem is script doesn't close connections correctly after DOWNLOAD_TIME seconds and continues downloading data for infinity in background.

import random
import requests
from concurrent.futures import ThreadPoolExecutor, as_completed
import time
import logging
import threading
import config

# Configure logging
logging.basicConfig(level=logging.INFO)

class TimeoutException(Exception):

    pass

def raiseTimeout():

    raise TimeoutException()

def show_status(status):

    print(status['connected'], 'proxies connected.', end='\r')

def download_with_proxy(proxy, urls, status, duration=config.TIMEOUT):
    proxy_str = f"{proxy['type']}://{proxy['host']}:{proxy['port']}"
    proxies_dict = {"http": proxy_str, "https": proxy_str}
    bytes_downloaded = 0

    for _ in range(config.CONNECTION_ATTEMPTS):
        try:
            url = random.choice(urls)
            headers = config.REQUEST_HEADERS
            connection_tried = time.time()
            with requests.get(url, proxies=proxies_dict, headers=headers, stream=True, timeout=duration) as response:

                if response.status_code == 200:
                    logging.info(f"Connected to {proxy['host']}:{proxy['port']}...\n")
                    proxy['actual_timeout'] = time.time() - connection_tried
                    status['connected'] += 1
                    show_status(status)
                    start_time = time.time()

                    if config.DOWNLOAD_TIME > 0:
                        download_timer = threading.Timer(config.DOWNLOAD_TIME, raiseTimeout)
                        try:
                            download_timer.start()
                            for chunk in response.iter_content(4096):
                                bytes_downloaded += len(chunk)
                        except TimeoutException as te:
                            pass
                        finally:
                            download_timer.cancel()

                    response.close()
                    proxy['mbps'] = round((bytes_downloaded / (time.time() - start_time)) / 1048576, 2)  # Calculate MBps
                    return proxy
        except requests.RequestException as e:
            logging.warning(f"Connection to {proxy['host']}:{proxy['port']} failed due to {e}\n")
            show_status(status)
    return False

def evaluate_proxies(proxies, urls, duration=config.TIMEOUT):
    results = []
    status = {'connected': 0}

    with ThreadPoolExecutor(max_workers=config.MAX_PARALLEL_CONNECTIONS) as executor:
        futures = {executor.submit(download_with_proxy, proxy, urls, status, duration): proxy for proxy in proxies}

        for future in as_completed(futures):
            proxy = future.result()
            if proxy is not False:
                results.append(proxy)

    return sorted(results, key=lambda d: d['mbps'])