#Please change the file extenstion from '.txt' to '.py' #import libraries import urllib2, sys #print information about author print \ """ --------------------------------- File Download Utility (Beta) Author: Nav Benipal Email: desireofstars@yahoo.com --------------------------------- """ #set Error to 0 Error = 0 #try to execute the following instructions try: #Get File Name and URL from first command Line Argument FileName = sys.argv[1] URLName = sys.argv[1] #check whether 'http://' is supplied or not try: IndexOfHttp = URLName.index("http://") except ValueError: URLName = "http://" + URLName #get URL and normalize it FileName = FileName.replace("http://","") FileName = FileName.replace("/","") FileName = FileName.replace("\\","") FileName = FileName.replace("?","") FileName = FileName.replace("@","") #Check the mode #Mode - Text('default') or Binary (if third command line argument is 'b') Mode = "" if len(sys.argv) >= 3: Mode = sys.argv[2] #Print a message print "Trying to Download " + sys.argv[1] #Create/Open file in respective mode if Mode == "b": fHandle = open((FileName.strip()+".txt"),'w+b') else: fHandle = open((FileName.strip()+".txt"),'w') #try to download the file try: for line in urllib2.urlopen(URLName): fHandle.write(line) except: Error = 1 print "Error: Please Check your URL." #Catch if something went wrong and print the usage except: print "Usage: FetchIt URL [mode]\n" print "Mode: " print " b - binary" print " default - text" Error = 2 if Error == 0: print "Downloading complete." elif Error == 1: print "Unable to Download." else: pass