Down_and_save_or_Delete = "save" #give save or delete. eg: Down_and_save_or_Delete = "delete"

destination_folder = r"C:\Users\Public\Desktop" ##  Here mention the path where the file should be saved on endpoint
filename = r"TeamViewerQS_x64.exe" #give filename with extension here ex: "setup.exe" or "file.pdf"
fromURL=r'https://download.teamviewer.com/download/TeamViewerQS_x64.exe' ## Here mention the download url for the file


import os
import ssl
import urllib2

Down_path = destination_folder

if not os.path.exists(destination_folder):
    os.makedirs(Down_path)

fileName = filename
DownTo = os.path.join(Down_path, fileName)


def downloadFile(DownTo, fromURL):
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'}
    context = ssl._create_unverified_context()
    request = urllib2.Request(fromURL, headers=headers)
    req = urllib2.urlopen(request,context=context)
    try:
        with open(DownTo, 'wb') as f:
            while True:
                chunk = req.read(100*1000*1000)
                if chunk:
                    f.write(chunk)
                else:
                    break
        if os.path.isfile(DownTo):
            print("Successfully downloaded the file.")
            print("The file is saved in this path: %s"%(DownTo))
            print('{} - {}KB'.format(DownTo, os.path.getsize(DownTo)/1024))
		
    except:
        raise Exception('Please Check URL or Download Path!')

if Down_and_save_or_Delete.lower() == "save":
    downloadFile(DownTo, fromURL)
elif Down_and_save_or_Delete.lower() == "delete":
    if os.path.exists(DownTo):
        print("%s file exists. Deleting the file"%(fileName))
        os.remove(DownTo)
        print("Successfully Deleted the file")
    else:
        print("%s file doesn't exists on the system"%(fileName))
else:
    print("Please check the spelling in this variable - Down_and_save_or_Delete")
