diff options
author | Andrés Delfino <34587441+andresdelfino@users.noreply.github.com> | 2018-04-16 14:02:56 (GMT) |
---|---|---|
committer | Senthil Kumaran <skumaran@gatech.edu> | 2018-04-16 14:02:56 (GMT) |
commit | c89b22175807d64c47b598163b804b5dc005d1bb (patch) | |
tree | 39a9fd773184e5840c3cf2822e4053fe8066b93b /Doc/howto/urllib2.rst | |
parent | b8e21f12891382bc0aac5ccd13dcb4a990d65e0a (diff) | |
download | cpython-c89b22175807d64c47b598163b804b5dc005d1bb.zip cpython-c89b22175807d64c47b598163b804b5dc005d1bb.tar.gz cpython-c89b22175807d64c47b598163b804b5dc005d1bb.tar.bz2 |
Remove to-be-deprecated urllib.request.urlretrieve function reference (#6454)
Diffstat (limited to 'Doc/howto/urllib2.rst')
-rw-r--r-- | Doc/howto/urllib2.rst | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/Doc/howto/urllib2.rst b/Doc/howto/urllib2.rst index 204a05a..ef1791c 100644 --- a/Doc/howto/urllib2.rst +++ b/Doc/howto/urllib2.rst @@ -56,12 +56,20 @@ The simplest way to use urllib.request is as follows:: with urllib.request.urlopen('http://python.org/') as response: html = response.read() -If you wish to retrieve a resource via URL and store it in a temporary location, -you can do so via the :func:`~urllib.request.urlretrieve` function:: +If you wish to retrieve a resource via URL and store it in a temporary +location, you can do so via the :func:`shutil.copyfileobj` and +:func:`tempfile.NamedTemporaryFile` functions:: + import shutil + import tempfile import urllib.request - local_filename, headers = urllib.request.urlretrieve('http://python.org/') - html = open(local_filename) + + with urllib.request.urlopen('http://python.org/') as response: + with tempfile.NamedTemporaryFile(delete=False) as tmp_file: + shutil.copyfileobj(response, tmp_file) + + with open(tmp_file.name) as html: + pass Many uses of urllib will be that simple (note that instead of an 'http:' URL we could have used a URL starting with 'ftp:', 'file:', etc.). However, it's the |