Skip to content

Instantly share code, notes, and snippets.

@MattSeen
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattSeen/8693139 to your computer and use it in GitHub Desktop.
Save MattSeen/8693139 to your computer and use it in GitHub Desktop.
'''
This code snippet was found on Stack Overflow
Question: http://stackoverflow.com/questions/34079/how-to-specify-an-authenticated-proxy-for-a-python-http-connection
Answer: http://stackoverflow.com/a/34116/1367612
'''
import urllib2
TRANSFER_PROTOCOL = "https"
PROXY_SERVER_ADDRESS = ""
USER_NAME = ""
PASSWORD = ""
PROXY_PORT = "8000"
def downloadFile(url):
'''
stupid doc string
'''
file_name = url.split('/')[-1]
proxy = urllib2.ProxyHandler(TRANSFER_PROTOCOL,
'{0}://{1}:{2}@{3}:{4}'.format(TRANSFER_PROTOCOL,
USER_NAME, PASSWORD, PROXY_SERVER_ADDRESS, PROXY_PORT))
auth = urllib2.HTTPBasicAuthHandler()
opener = urllib2.build_opener(proxy, auth, urllib2.HTTPHandler)
urllib2.install_opener(opener)
opend_url = urllib2.urlopen(url)
file_handle = open(file_name, 'wb')
meta = opend_url.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 = opend_url.read(block_sz)
if not buffer:
break
file_size_dl += len(buffer)
file_handle.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,
file_handle.close()
URLS = [
# files, comma seperated, that you want to download.
]
for url in URLS:
downloadFile(url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment