from urllib.request import urlopen
from contextlib import closing


def save_to_file(url, file_path):
    with closing(urlopen(url)) as request:
        with open(file_path, 'wb') as f:
            f.write(request.read())

from contextlib import closing
from urllib.request import urlopen, OpenerDirector


def save_to_file(url: str, file_path: str, opener: OpenerDirector = None):
    def process(request_):
        with open(file_path, 'wb') as f:
            f.write(request_.read())

    if opener:
        with closing(opener.open(url)) as request:
            process(request)
    else:
        with closing(urlopen(url)) as request:
            process(request)

Add opener support to lib.download.save_to_file