import os import urllib2 import urllib def url_exists(URL): try: request = urllib2.Request(URL) urllib2.urlopen(request) return True except Exception: raise urllib2.URLError("ERROR: failed to connect to the URL: " + URL) def download_from_url(url, local_folder): # strip leading or trailing whitespaces or '/' from inputs url and local_folder url = url.strip().strip('/') local_folder = local_folder.strip() # Get the name of the file to be downloaded i = url.rindex('/') installer = url[i+1:] # Get the path of the file on the local machine by appending filename to the path local_folder local_path = os.path.join(local_folder, installer) # Create local_folder if it doesn't exist already if not os.path.exists(local_folder): print 'Directory doesn\'t exist. Creating: ' + local_folder os.makedirs(local_folder) # If the file 'local_path' does not exist, Download the file from the url if not os.path.exists(local_path): print 'File doesn\'t exist. Downloading: ' + url if url_exists(url): # Get only filename from urlretrieve tuple (filename, headers) filename = urllib.urlretrieve(url, local_path)[0] return filename else: return local_path def main(): url1 = '' url2 = '' tmpDir = os.getcwd() #download file 1 try: file1_path = download_from_url(url1, local_folder=tmpDir) except Exception as e: print e exit(-1) #download file 2 try: file2_path = download_from_url(url2, local_folder=tmpDir) except Exception as e: print e exit(-1) return if __name__ == "__main__": main()