#!/usr/bin/python import random import urllib, urllib2 urlopen = urllib2.urlopen Request = urllib2.Request import sys MAX_SIZE = 1024 * 1024 * 1.2 ###___this somewhat poorly written program basically performs a get request #you can specify three arguments, the url, the destination path and the filename#that you'll want your file to be called #these can be specified as argument values from the commandline or as #values in the mainfunction which is downloader.download(url,filename,destination) #the destination and filename arguments are optional url = "" destination = "" filename = "" def argval(): global destination global url global filename stuff = sys.argv lenstuff = len(stuff) if lenstuff >= 4: getvalues() if lenstuff ==3: print str(lenstuff) url = sys.argv[-2] filename = sys.argv[-1] if lenstuff ==2: url = sys.argv[-1] destination = "" partz = url.split('/') filename = partz[-1] if lenstuff ==1: print 'please provide url' return None return url def getvalues(): global url global destination global filename url = sys.argv[-3] destination = sys.argv[-1] filename = sys.argv[-2] if len(filename) == 0: partz = url.split('/') filename = partz[-1] if filename == "" or filename == None: partz = url.split('/') filename = partz[-1] return url def browser_request (url,data=None): headers = { 'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)', 'Accept': '*/*', } try: req = Request(url, data, headers) response = urlopen(req) return response except: print "ERROR" sys.stdout.write('there is a problem with the url or an I/O error') return None sys.exit() def download(url, filename="",destination=""): response = browser_request(url) rawimg = response.read() if len(rawimg) == 0: print error("got zero-length file") sys.stdout.write("file did not exist or was zero-length") sys.exit() if len(rawimg) > MAX_SIZE and "asdf.us" not in url: error = "file too big: max size " + str(MAX_SIZE/1024) + " KB / " + filename + " is " + str(len(rawimg)/1024) + " KB" print error sys.stdout.write(error) sys.exit() if filename == "": partz = url.split('/') filename = partz[-1] path = "" if destination == "" or destination == '': path = filename else: path = destination+filename f = open(path, "w") f.write(rawimg) print 'success' f.close() finalitems = {} finalitems['url'] = url finalitems['filename'] = filename finalitems['destination'] = destination return finalitems def downloader(): global url global destination global filename argval() finalitems = download(url, filename, destination) return finalitems if __name__ == '__main__': downloader()