@@ -1060,8 +1060,9 @@ This example gets the python.org main page and displays the first 300 bytes of
10601060it. ::
10611061
10621062 >>> import urllib.request
1063- >>> f = urllib.request.urlopen('http://www.python.org/')
1064- >>> print(f.read(300))
1063+ >>> with urllib.request.urlopen('http://www.python.org/') as f:
1064+ ... print(f.read(300))
1065+ ...
10651066 b'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
10661067 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n\n\n<html
10671068 xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">\n\n<head>\n
@@ -1103,8 +1104,9 @@ when the Python installation supports SSL. ::
11031104 >>> import urllib.request
11041105 >>> req = urllib.request.Request(url='https://localhost/cgi-bin/test.cgi',
11051106 ... data=b'This data is passed to stdin of the CGI')
1106- >>> f = urllib.request.urlopen(req)
1107- >>> print(f.read().decode('utf-8'))
1107+ >>> with urllib.request.urlopen(req) as f:
1108+ ... print(f.read().decode('utf-8'))
1109+ ...
11081110 Got Data: "This data is passed to stdin of the CGI"
11091111
11101112The code for the sample CGI used in the above example is::
@@ -1119,7 +1121,8 @@ Here is an example of doing a ``PUT`` request using :class:`Request`::
11191121 import urllib.request
11201122 DATA=b'some data'
11211123 req = urllib.request.Request(url='http://localhost:8080', data=DATA,method='PUT')
1122- f = urllib.request.urlopen(req)
1124+ with urllib.request.urlopen(req) as f:
1125+ pass
11231126 print(f.status)
11241127 print(f.reason)
11251128
@@ -1185,8 +1188,10 @@ containing parameters::
11851188 >>> import urllib.request
11861189 >>> import urllib.parse
11871190 >>> params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
1188- >>> f = urllib.request.urlopen("http://www.musi-cal.com/cgi-bin/query?%s" % params)
1189- >>> print(f.read().decode('utf-8'))
1191+ >>> url = "http://www.musi-cal.com/cgi-bin/query?%s" % params
1192+ >>> with urllib.request.urlopen(url) as f:
1193+ ... print(f.read().decode('utf-8'))
1194+ ...
11901195
11911196The following example uses the ``POST `` method instead. Note that params output
11921197from urlencode is encoded to bytes before it is sent to urlopen as data::
@@ -1198,24 +1203,27 @@ from urlencode is encoded to bytes before it is sent to urlopen as data::
11981203 >>> request = urllib.request.Request("http://requestb.in/xrbl82xr")
11991204 >>> # adding charset parameter to the Content-Type header.
12001205 >>> request.add_header("Content-Type","application/x-www-form-urlencoded;charset=utf-8")
1201- >>> f = urllib.request.urlopen(request, data)
1202- >>> print(f.read().decode('utf-8'))
1206+ >>> with urllib.request.urlopen(request, data) as f:
1207+ ... print(f.read().decode('utf-8'))
1208+ ...
12031209
12041210The following example uses an explicitly specified HTTP proxy, overriding
12051211environment settings::
12061212
12071213 >>> import urllib.request
12081214 >>> proxies = {'http': 'http://proxy.example.com:8080/'}
12091215 >>> opener = urllib.request.FancyURLopener(proxies)
1210- >>> f = opener.open("http://www.python.org")
1211- >>> f.read().decode('utf-8')
1216+ >>> with opener.open("http://www.python.org") as f:
1217+ ... f.read().decode('utf-8')
1218+ ...
12121219
12131220The following example uses no proxies at all, overriding environment settings::
12141221
12151222 >>> import urllib.request
12161223 >>> opener = urllib.request.FancyURLopener({})
1217- >>> f = opener.open("http://www.python.org/")
1218- >>> f.read().decode('utf-8')
1224+ >>> with opener.open("http://www.python.org/") as f:
1225+ ... f.read().decode('utf-8')
1226+ ...
12191227
12201228
12211229Legacy interface
0 commit comments