Skip to content

Instantly share code, notes, and snippets.

@mavitm
Last active April 5, 2016 12:30
Show Gist options
  • Save mavitm/9096f49d5bb2dd6d91286f4ef749008b to your computer and use it in GitHub Desktop.
Save mavitm/9096f49d5bb2dd6d91286f4ef749008b to your computer and use it in GitHub Desktop.

Python 2.7.6 urllib2 File Download

import sys
import os
import urllib2, cookielib, Cookie

url = sys.argv[1]
#ref = sys.argv[2]

if url.find("http") != -1:

	file_name = url.split('/')[-1]

	cj = cookielib.LWPCookieJar()
	#cj.load('cookies.txt')

	opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
	opener.addheaders = [
		('Host','192.168.1.1'),
		('Accept','text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
		('Accept-language','en-US,en;q=0.5'),
		#('Accept-encoding','gzip, deflate'),
		('User-agent', 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0'),
		('Referer', 'https://goo.gl/'),
		('Accept-Charset', 'ISO-8859-9,utf-8;q=0.7,*;q=0.7'),
        ('Keep-Alive', '115'),
        ('Connection', 'keep-alive'),
        ('Cache-Control', 'max-age=0'),
	]

	u = opener.open(url)
	f = open(file_name, 'wb')
	meta = u.info()
	file_size = int(meta.getheaders("Content-Length")[0])
	print "Downloading: %s Bytes: %s" % (file_name, file_size)

	file_size_dl = 0
	block_sz = 8192
	while True:
	    buffer = u.read(block_sz)
	    if not buffer:
	        break

	    file_size_dl += len(buffer)
	    f.write(buffer)
	    status = r"%10d  [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
	    status = status + chr(8)*(len(status)+1)
	    print status,

	f.close()

else:
	print("urlyi adam gibi yaz")

Example

$ python file.py httpt://www.targetfilesource.com/file.tar.gz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment