@contextmanager<EOL>def url_as_file(url, ext=None):
if ext:<EOL><INDENT>ext = '<STR_LIT:.>' + ext.strip('<STR_LIT:.>')  <EOL><DEDENT>url_hint = '<STR_LIT>'.format(urlparse(url).hostname or '<STR_LIT>')<EOL>content = requests.get(url).content<EOL>with tempfile.NamedTemporaryFile(suffix=ext or '<STR_LIT>', prefix=url_hint, delete=False) as handle:<EOL><INDENT>handle.write(content)<EOL><DEDENT>try:<EOL><INDENT>yield handle.name<EOL><DEDENT>finally:<EOL><INDENT>if os.path.exists(handle.name):<EOL><INDENT>os.remove(handle.name)<EOL><DEDENT><DEDENT>
Context manager that GETs a given `url` and provides it as a local file.

The file is in a closed state upon entering the context,
and removed when leaving it, if still there.

To give the file name a specific extension, use `ext`;
the extension can optionally include a separating dot,
otherwise it will be added.

Parameters:
    url (str): URL to retrieve.
    ext (str, optional): Extension for the generated filename.

Yields:
    str: The path to a temporary file with the content of the URL.

Raises:
    requests.RequestException: Base exception of ``requests``, see its
        docs for more detailed ones.

Example:
    >>> import io, re, json
    >>> with url_as_file('https://api.github.com/meta', ext='json') as meta:
    ...     meta, json.load(io.open(meta, encoding='ascii'))['hooks']
    (u'/tmp/www-api.github.com-Ba5OhD.json', [u'192.30.252.0/22'])