def _download_segments(filename, url, segments):
    """download segments into a single file."""
    gribfile = open(filename, 'w')
    for start, end in segments:
        req = urllib2.Request(url)
        req.add_header('User-Agent',
                       'caelum/0.1 +https://github.com/nrcharles/caelum')
        if end:
            req.headers['Range'] = 'bytes=%s-%s' % (start, end)
        else:
            req.headers['Range'] = 'bytes=%s' % (start)
        opener = urllib2.build_opener()
        gribfile.write(opener.open(req).read())
    gribfile.close()