def url_download(url, topath=None, create_dirs=True):
<EOL>if topath: topath = os.path.expanduser(os.path.expandvars(topath))<EOL>if create_dirs and topath:<EOL><INDENT>dnm = os.path.dirname(topath)<EOL>if not os.path.isdir(dnm): os.makedirs(os.path.abspath(dnm), <NUM_LIT>)<EOL><DEDENT>if six.PY2:<EOL><INDENT>response = urllib.request.urlopen(url)<EOL>if topath is None: topath = response.read()<EOL>else:<EOL><INDENT>with open(topath, '<STR_LIT:wb>') as fl:<EOL><INDENT>shutil.copyfileobj(response, fl)<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>with urllib.request.urlopen(url) as response:<EOL><INDENT>if topath is None: topath = response.read()<EOL>else:<EOL><INDENT>with open(topath, '<STR_LIT:wb>') as fl:<EOL><INDENT>shutil.copyfileobj(response, fl)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>return topath<EOL>
url_download(url) yields the contents of the given url as a byte-string.
url_download(url, topath) downloads the given url to the given path, topath and yields that path
  on success.

The option create_dirs (default: True) may be set to False to prevent the topath directory from
being created.