Skip to content

Instantly share code, notes, and snippets.

@mikegreiling
Created July 8, 2014 21:27
Show Gist options
  • Save mikegreiling/1d831303be2a29ebc39f to your computer and use it in GitHub Desktop.
Save mikegreiling/1d831303be2a29ebc39f to your computer and use it in GitHub Desktop.
Download all available versions of aws 'opsworks-agent-installer.tgz' for examination
#!/usr/bin/env python
import os, time, urllib2, shutil, urlparse
def main():
for version in range(450):
try:
url = 'https://opsworks-instance-agent.s3.amazonaws.com:443/%d/opsworks-agent-installer.tgz' % version
filename = 'opsworks-agent-installer-%03d.tgz' % version
download(url, filename)
print('version %d successfully downloaded' % version)
except urllib2.HTTPError, e:
print('version %d does not exist' % version)
def download(url, fileName=None):
def getFileName(url,openUrl):
filename = None
if 'Content-Disposition' in openUrl.info():
cd = dict(map(
lambda x: x.strip().split('=') if '=' in x else (x.strip(),''),
openUrl.info()['Content-Disposition'].split(';')))
if 'filename' in cd:
filename = cd['filename'].strip("\"'")
return filename or os.path.basename(urlparse.urlsplit(openUrl.url)[2])
def getLastModified(url,openUrl):
mTime = None
if 'Last-Modified' in openUrl.info():
lmStr = openUrl.info()['Last-Modified']
mTime = time.mktime(time.strptime(lmStr, "%a, %d %b %Y %H:%M:%S GMT"))
return mTime or time.time()
r = urllib2.urlopen(urllib2.Request(url))
try:
fileName = fileName or getFileName(url,r)
lastModified = getLastModified(url,r)
with open(fileName, 'wb') as f:
shutil.copyfileobj(r,f)
os.utime(fileName, (lastModified, lastModified))
finally:
r.close()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print "\nOpteration Aborted\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment