Skip to content

Instantly share code, notes, and snippets.

@cldellow
Created June 10, 2012 16:19
Show Gist options
  • Select an option

  • Save cldellow/2906444 to your computer and use it in GitHub Desktop.

Select an option

Save cldellow/2906444 to your computer and use it in GitHub Desktop.
A poor man's download accelerator
import getopt, sys, os, re
optlist, args = getopt.getopt(sys.argv[1:], "c:")
if len(args) != 2:
print "usage: " + sys.argv[0] + " [-c cookie] <url> <file> | bash"
sys.exit(1)
url, file = args
cookie = ""
if len(optlist) == 1 and optlist[0][0] == '-c':
cookie = " -H 'Cookie: " + optlist[0][1] + "' "
content_length_re = re.compile("Content-Length: ([0-9]+)")
bytes = -1
stream = os.popen("curl --silent --head " + cookie + " '" + url + "'")
for line in stream:
match = content_length_re.match(line)
if match:
bytes = int(match.group(1))
if bytes == -1:
print "could not get content-length"
sys.exit(1)
chunks = 8
chunk_size = ((bytes / chunks) / 1024) * 1024
offset, seek = [0, 0]
for i in range(chunks):
range = "Range: bytes=" + str(offset) + "-" + str(offset + chunk_size - 1)
if i == chunks - 1:
range = "Range: bytes=" + str(offset) + "-"
print "curl --silent -H \"" + range + "\" " + cookie + " '" + url + "' | dd status=noxfer iflag=binary oflag=binary obs=1024 seek=" + str(seek) + " of=" + file + "
&"
seek = seek + (chunk_size / 1024)
offset = offset + chunk_size
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment