summaryrefslogtreecommitdiff
path: root/Doc/library/urllib.request.rst
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/library/urllib.request.rst
parent8ad751e0241b7bfbdfacff017c47794b3b0a3211 (diff)
downloadcpython-git-9575e1891ff533318f6dd72ab6f6bd0f9a042014.tar.gz
Issue #12955: Change the urlopen() examples to use context managers where appropriate.
Patch by Martin Panter.
Diffstat (limited to 'Doc/library/urllib.request.rst')
-rw-r--r--Doc/library/urllib.request.rst34
1 files changed, 21 insertions, 13 deletions
diff --git a/Doc/library/urllib.request.rst b/Doc/library/urllib.request.rst
index 249396e25f..d878aac557 100644
--- a/Doc/library/urllib.request.rst
+++ b/Doc/library/urllib.request.rst
@@ -1048,8 +1048,9 @@ This example gets the python.org main page and displays the first 300 bytes of
it. ::
>>> import urllib.request
- >>> f = urllib.request.urlopen('http://www.python.org/')
- >>> print(f.read(300))
+ >>> with urllib.request.urlopen('http://www.python.org/') as f:
+ ... print(f.read(300))
+ ...
b'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n\n\n<html
xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">\n\n<head>\n
@@ -1091,8 +1092,9 @@ when the Python installation supports SSL. ::
>>> import urllib.request
>>> req = urllib.request.Request(url='https://localhost/cgi-bin/test.cgi',
... data=b'This data is passed to stdin of the CGI')
- >>> f = urllib.request.urlopen(req)
- >>> print(f.read().decode('utf-8'))
+ >>> with urllib.request.urlopen(req) as f:
+ ... print(f.read().decode('utf-8'))
+ ...
Got Data: "This data is passed to stdin of the CGI"
The code for the sample CGI used in the above example is::
@@ -1107,7 +1109,8 @@ Here is an example of doing a ``PUT`` request using :class:`Request`::
import urllib.request
DATA=b'some data'
req = urllib.request.Request(url='http://localhost:8080', data=DATA,method='PUT')
- f = urllib.request.urlopen(req)
+ with urllib.request.urlopen(req) as f:
+ pass
print(f.status)
print(f.reason)
@@ -1173,8 +1176,10 @@ containing parameters::
>>> import urllib.request
>>> import urllib.parse
>>> params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
- >>> f = urllib.request.urlopen("http://www.musi-cal.com/cgi-bin/query?%s" % params)
- >>> print(f.read().decode('utf-8'))
+ >>> url = "http://www.musi-cal.com/cgi-bin/query?%s" % params
+ >>> with urllib.request.urlopen(url) as f:
+ ... print(f.read().decode('utf-8'))
+ ...
The following example uses the ``POST`` method instead. Note that params output
from urlencode is encoded to bytes before it is sent to urlopen as data::
@@ -1186,8 +1191,9 @@ from urlencode is encoded to bytes before it is sent to urlopen as data::
>>> request = urllib.request.Request("http://requestb.in/xrbl82xr")
>>> # adding charset parameter to the Content-Type header.
>>> request.add_header("Content-Type","application/x-www-form-urlencoded;charset=utf-8")
- >>> f = urllib.request.urlopen(request, data)
- >>> print(f.read().decode('utf-8'))
+ >>> with urllib.request.urlopen(request, data) as f:
+ ... print(f.read().decode('utf-8'))
+ ...
The following example uses an explicitly specified HTTP proxy, overriding
environment settings::
@@ -1195,15 +1201,17 @@ environment settings::
>>> import urllib.request
>>> proxies = {'http': 'http://proxy.example.com:8080/'}
>>> opener = urllib.request.FancyURLopener(proxies)
- >>> f = opener.open("http://www.python.org")
- >>> f.read().decode('utf-8')
+ >>> with opener.open("http://www.python.org") as f:
+ ... f.read().decode('utf-8')
+ ...
The following example uses no proxies at all, overriding environment settings::
>>> import urllib.request
>>> opener = urllib.request.FancyURLopener({})
- >>> f = opener.open("http://www.python.org/")
- >>> f.read().decode('utf-8')
+ >>> with opener.open("http://www.python.org/") as f:
+ ... f.read().decode('utf-8')
+ ...
Legacy interface