aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBerker Peksag <berker.peksag@gmail.com>2015-04-12 13:52:49 +0300
committerBerker Peksag <berker.peksag@gmail.com>2015-04-12 13:52:49 +0300
commit9575e1891ff533318f6dd72ab6f6bd0f9a042014 (patch)
treeb9ebbcf4b0db1eb78f209b2b5ec26a623cfda1fc /Doc/howto/urllib2.rst
parentClose #23904: fix pathlib documentation misleadingly mentioning that bytes ob... (diff)
downloadcpython-9575e1891ff533318f6dd72ab6f6bd0f9a042014.tar.gz
cpython-9575e1891ff533318f6dd72ab6f6bd0f9a042014.tar.bz2
cpython-9575e1891ff533318f6dd72ab6f6bd0f9a042014.zip
Issue #12955: Change the urlopen() examples to use context managers where appropriate.
Patch by Martin Panter.
Diffstat (limited to 'Doc/howto/urllib2.rst')
-rw-r--r--Doc/howto/urllib2.rst16
1 files changed, 8 insertions, 8 deletions
diff --git a/Doc/howto/urllib2.rst b/Doc/howto/urllib2.rst
index abec0539004..01ae513644a 100644
--- a/Doc/howto/urllib2.rst
+++ b/Doc/howto/urllib2.rst
@@ -53,8 +53,8 @@ Fetching URLs
The simplest way to use urllib.request is as follows::
import urllib.request
- response = urllib.request.urlopen('http://python.org/')
- html = response.read()
+ 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::
@@ -79,8 +79,8 @@ response::
import urllib.request
req = urllib.request.Request('http://www.voidspace.org.uk')
- response = urllib.request.urlopen(req)
- the_page = response.read()
+ with urllib.request.urlopen(req) as response:
+ the_page = response.read()
Note that urllib.request makes use of the same Request interface to handle all URL
schemes. For example, you can make an FTP request like so::
@@ -117,8 +117,8 @@ library. ::
data = urllib.parse.urlencode(values)
data = data.encode('utf-8') # data should be bytes
req = urllib.request.Request(url, data)
- response = urllib.request.urlopen(req)
- the_page = response.read()
+ with urllib.request.urlopen(req) as response:
+ the_page = response.read()
Note that other encodings are sometimes required (e.g. for file upload from HTML
forms - see `HTML Specification, Form Submission
@@ -183,8 +183,8 @@ Explorer [#]_. ::
data = urllib.parse.urlencode(values)
data = data.encode('utf-8')
req = urllib.request.Request(url, data, headers)
- response = urllib.request.urlopen(req)
- the_page = response.read()
+ with urllib.request.urlopen(req) as response:
+ the_page = response.read()
The response also has two useful methods. See the section on `info and geturl`_
which comes after we have a look at what happens when things go wrong.