[Python-checkins] Remove to-be-deprecated urllib.request.urlretrieve function reference (GH-6454)

Miss Islington (bot) webhook-mailer at python.org
Mon Apr 16 10:22:02 EDT 2018


https://github.com/python/cpython/commit/ab99475b6621b7ea0fc01056e9595c9706136492
commit: ab99475b6621b7ea0fc01056e9595c9706136492
branch: 3.6
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2018-04-16T07:21:57-07:00
summary:

Remove to-be-deprecated urllib.request.urlretrieve function reference (GH-6454)

(cherry picked from commit c89b22175807d64c47b598163b804b5dc005d1bb)

Co-authored-by: Andrés Delfino <34587441+andresdelfino at users.noreply.github.com>

files:
M Doc/howto/urllib2.rst

diff --git a/Doc/howto/urllib2.rst b/Doc/howto/urllib2.rst
index edf0abd5c5d2..8b93d701869d 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



More information about the Python-checkins mailing list