import os import subprocess import ctypes import urllib2 import ssl download_url = 'https://cdn.pwm.jumpcloud.com/DA/release/JumpCloud-Password-Manager-latest.exe' download_path = 'C:\\ProgramData\\JumpCloud-Password-Manager-latest' ## provide your path here where the file has to been downloaded file_name = 'JumpCloud-Password-Manager-latest.exe' ## provide your file name with extention if os.path.exists(download_path): pass else: os.makedirs(download_path) class disable_file_system_redirection: _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection def __enter__(self): self.old_value = ctypes.c_long() self.success = self._disable(ctypes.byref(self.old_value)) def __exit__(self, type, value, traceback): if self.success: self._revert(self.old_value) with disable_file_system_redirection(): def download(fromURL,DownTo): print(file_name+' Downloading......') 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(file_name+" Downloaded...") return "Success" except: return 'Please Check URL or Download Path!' def install(cmd): obj = subprocess.Popen(cmd, shell=True, stdout = subprocess.PIPE, stderr = subprocess.PIPE) out, err = obj.communicate() if err: print(err) else: print(file_name+' installed successfully...') src_path = download_path+'\\'+file_name Download = download(download_url,src_path) if Download == "Success": command = '"'+src_path+'" /VERYSILENT' Install = install(command) else: print("Error occurred while downloading "+file_name)