import requestsimport osimport threading# فهرست آدرسهای فایلهاfile_urls = [f"https://dummyimage.com/{i}.png"for i inrange(300, 400)]# ایجاد پوشه downloads اگر وجود نداشته باشدifnot os.path.exists("downloads"): os.makedirs("downloads")# Semaphore برای محدود کردن تعداد تردهای فعال همزمانmax_threads =5thread_limit = threading.BoundedSemaphore(max_threads)defdownload_file(file_url, cookies, headers):# با استفاده از Semaphore برای کنترل تعداد تردهاwith thread_limit: file_name = os.path.basename(file_url) file_path = os.path.join("downloads", file_name)try:print(f"Downloading {file_name}...") response = requests.get(file_url, cookies=cookies, headers=headers, timeout=10) response.raise_for_status() # اگر خطای HTTP رخ دهدwithopen(file_path, "wb") as f: f.write(response.content)print(f"{file_name} downloaded successfully.")exceptExceptionas e:print(f"❌ Error downloading {file_name}: {e}")# ساخت و مدیریت تردهاthreads = []for file_url in file_urls: t = threading.Thread(target=download_file, args=(file_url, {}, {})) threads.append(t) t.start()# صبر کردن تا تمام تردها کارشان را تمام کنندfor t in threads: t.join()print("✅ All files downloaded successfully.")